Advertisement
1700. Number of Students Unable to Eat Lunch
EasyView on LeetCode
Time: O(n)
Space: O(1)
Approach
Count student preferences; simulate serving sandwiches top-of-stack; stop when no match possible.
1700.cs
C#
// Approach: Count student preferences; simulate serving sandwiches top-of-stack; stop when no match possible.
// Time: O(n) Space: O(1)
public class Solution
{
public int CountStudents(int[] students, int[] sandwiches)
{
int[] count = new int[2];
foreach (int student in students)
++count[student];
for (int i = 0; i < sandwiches.Length; ++i)
{
if (count[sandwiches[i]] == 0)
return sandwiches.Length - i;
--count[sandwiches[i]];
}
return 0;
}
}Advertisement
Was this solution helpful?