Leetcode Note: Go - Number of Students Unable to Eat Lunch

Number of Students Unable to Eat Lunch - LeetCode
https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/

  • Go 言語で取り組んだメモ

回答

Go/C O(n) 0ms, 2.2MB/5.9MB (100%/100%), Swift 8 ms - Number of Students Unable to Eat Lunch - LeetCode
https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/solutions/990678/go-c-o-n-0ms-2-2mb-5-9mb-100-100-swift-8-ms/

func countStudents(students []int, sandwiches []int) int {
    var counters [2]int
	// count zeros and ones in students
    for i := 0; i < len(students); i++ {
        counters[students[i]]++
    }
	// go over the sandwiches queue
    for i := 0; i < len(sandwiches); i++ {
        if counters[sandwiches[i]] == 0 {
            break  // stop if there is no student of the kind left
        }
        counters[sandwiches[i]]--  // decrement number of students of the kind
    }
    // return number of students left
	return counters[0] + counters[1]
}