2 Easy Interview Approaches ✅ || Brute Force and Optimal Solution 🔥|| C++ || Java || Python
Intuition\nBrute Force Solution\n\n# Approach\n Describe your approach to solving the problem. \n using Queue and Stack\n\n# Complexity\n- Time complexity: O(n+
# Intuition\nBrute Force Solution\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n using Queue and Stack\n\n# Complexity\n- Time complexity: O(n+n)  i.e-> O(n) \n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    int countStudents(vector<int>& students, vector<int>& sandwiches) {\n        queue<int>q;\n        stack<int>st;\n\n        int cnt=0;\n\n        for(int i=0; i<students.size(); i++)\n        {\n            q.push(students[i]);\n        }\n        for(int j=sandwiches.size()-1; j>=0; j--)\n        {\n        st.push(sandwiches[j]);\n        }\n\n        while(!st.empty() and cnt<q.size())\n        {\n            if(q.front()!=st.top()){\n            q.push(q.front());\n            q.pop();\n\n            cnt++;\n            }\n            else\n            {\n                q.pop();\n                st.pop();\n                cnt=0;\n            }   \n        }\n\n        return st.size();\n    }\n};\n```\n```Java []\nclass Solution {\n    public int countStudents(int[] students, int[] sandwiches) {\n        Queue<Integer> q = new LinkedList<>();\n        Stack<Integer> st = new Stack<>();\n\n        int cnt = 0;\n\n        for (int student : students) {\n            q.offer(student);\n        }\n        for (int j = sandwiches.length - 1; j >= 0; j--) {\n            st.push(sandwiches[j]);\n        }\n\n        while (!st.empty() && cnt < q.size()) {\n            if (q.peek() != st.peek()) {\n                q.offer(q.poll());\n                cnt++;\n            } else {\n                q.poll();\n                st.pop();\n                cnt = 0;\n            }\n        }\n\n        return st.size();\n    }\n}\n\n```\n```python []\nclass Solution(object):\n    def countStudents(self, students, sandwiches):\n        q = []\n        st = []\n\n        cnt = 0\n\n        for student in students:\n            q.append(student)\n        for sandwich in reversed(sandwiches):\n            st.append(sandwich)\n\n        while st and cnt < len(q):\n            if q[0] != st[-1]:\n                q.append(q.pop(0))\n                cnt += 1\n            else:\n                q.pop(0)\n                st.pop()\n                cnt = 0\n\n        return len(st)\n\n```\n# Intuition\nOptimal Approach\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n by takeing the one\'s and zero\'s Count in vector \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\n# Code\n```C++ []\nclass Solution {\npublic:\n    int countStudents(vector<int>& students, vector<int>& sandwiches) {\n        vector<int>count(2,0);\n\n        for(auto student : students)\n        count[student]++;\n\n        int remain = sandwiches.size();\n        for(auto sandwich : sandwiches)\n        {\n            if(count[sandwich]==0)\n            break;\n\n            if(remain==0)\n            break;\n\n            count[sandwich]--;\n            remain--;\n        }\n\n        return remain;\n\n    }\n};\n```\n```Java []\nclass Solution {\n    public int countStudents(int[] students, int[] sandwiches) {\n        int[] count = new int[2];\n\n        for (int student : students)\n            count[student]++;\n\n        int remain = sandwiches.length;\n        for (int sandwich : sandwiches) {\n            if (count[sandwich] == 0)\n                break;\n\n            if (remain == 0)\n                break;\n\n            count[sandwich]--;\n            remain--;\n        }\n\n        return remain;\n    }\n}\n\n```\n```python []\nclass Solution(object):\n    def countStudents(self, students, sandwiches):\n        count = [0, 0]\n\n        for student in students:\n            count[student] += 1\n\n        remain = len(sandwiches)\n        for sandwich in sandwiches:\n            if count[sandwich] == 0:\n                break\n\n            if remain == 0:\n                break\n\n            count[sandwich] -= 1\n            remain -= 1\n\n        return remain\n\n\n```\n![upvote cat.png](https://assets.leetcode.com/users/images/c6859306-f222-4e07-b65d-78ddd96c5af8_1712560733.6768203.png)\n\n
['Stack', 'Queue', 'Python', 'C++', 'Java']