0ms - Beats 100% 🔥🔥🔥 | Python, C++ 💻 | EASY - CONSTANT Space - Clear explanation 📗
Proof\n Describe your first thoughts on how to solve this problem. \n### 1. C ++\n\n### 2. Python3\n\n\n# Code (with explanation each line)\npython3 []\nclass S
# Proof\n<!-- Describe your first thoughts on how to solve this problem. -->\n### 1. C ++\n![image.png](https://assets.leetcode.com/users/images/f57fb3b4-2924-48a5-bbdf-3e5e05b24b17_1712678263.9577212.png)\n### 2. Python3\n![image.png](https://assets.leetcode.com/users/images/4746671e-e24c-4ec5-8052-f6ab7767a85b_1712678275.8687181.png)\n\n# Code (with explanation each line)\n```python3 []\nclass Solution:\n    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n        ones = sum(students) # Number of students prefer to eat square sandwich (sandwich 1)\n        zeros = len(students) - ones # Number of students prefer to eat circle sandwich (sandwich 0)\n        for s in sandwiches:\n            if s == 1: # If sandwich 1 at top of stack\n                if ones == 0: # And NO student who prefers sandwich 1 anymore -> NOT a single student can take the sandwich 1 at top of stack\n                    return zeros # Then students who prefer sandwich 0 can\'t have their lunch\n                ones -= 1 # If there\'re still students who prefer sandwich 1 then those students can take the sandwich at top of stack\n            else: # If sandwich 0 at top of stack\n                if zeros == 0: # And NO one can take sandwich 0 at top of stack -> sandwiches 1 (if exist in stack but not at top) CANNOT be taken also\n                    return ones # Then students who prefer sandwich 1 can\'t have their lunch\n                zeros -= 1 # If there\'re still students who prefer sandwich 0 then those students can take the sandwich at top of stack\n        return 0 # No student has to suffer from starving\n```\n```cpp []\nclass Solution {\npublic:\n    int countStudents(vector<int>& students, vector<int>& sandwiches) {\n        int ones = accumulate(students.begin(), students.end(), 0);\n        int zeros = students.size() - ones;\n        for (int s : sandwiches) {\n            if (s == 1) {\n                if (ones == 0) {\n                    return zeros;\n                }\n                ones--;\n            }\n            else {\n                if (zeros == 0) {\n                    return ones;\n                }\n                zeros--;\n            }\n        }\n        return 0;\n    }\n};\n```\n# Complexity\n- Time complexity: $$O(N)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n## Upvote if you find this solution helpful, thank you \uD83E\uDD0D
['Simulation', 'C++', 'Python3']