JavaScript - 97% Faster - 93% Less Memory - O(n) - No Queue/Stack.
Intuition\n Describe your first thoughts on how to solve this problem. \nBecause of the queue-like behavior exibited by the Students, we do not actually have to
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBecause of the queue-like behavior exibited by the Students, we do not actually have to use a queue. Instead, we should count the number of students that prefer a 1 and 0 respectively.\n\nThen we iterate through the sandwiches array and decrement for each preference consumed. If it drops below 0 in any case then it can be proven that no more can be consumed and the result is the length of the array minus the index we are on.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Count the students for 1s and 0s\n- Loop through sandwiches. Decrement from above variables if a match is found\n- If we are less than 0 on a decrement, then the rest cannot be fed. Return length of array minus current index\n- If we go through loop successfully, return 0 as everyone has eaten\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThis requires linear time $$O(n)$$ both loops are iterated through only once.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThis requires constant space $$O(1)$$. Only counting variables needed.\n\n# Code\n```\n/**\n * @param {number[]} students\n * @param {number[]} sandwiches\n * @return {number}\n */\nvar countStudents = function(students, sandwiches) {\n    let studentsOnes = 0;\n    let studentsZeros = 0;\n\n    for (let i = 0; i < students.length; i++) {\n        if (students[i] == 1)\n            studentsOnes++;\n        else\n            studentsZeros++;\n    }\n\n    for (let i = 0; i < sandwiches.length; i++) {\n        if (sandwiches[i] == 1) {\n            studentsOnes--;\n            if (studentsOnes < 0)\n                return sandwiches.length - i;\n        } else {\n            studentsZeros--;\n            if (studentsZeros < 0)\n                return sandwiches.length - i;\n        }\n    }\n\n    return 0;\n};\n```
['JavaScript']