2 Interview Approaches with video Solution (Explained Question clearly)🔥  || Brute->Optimal ✅
Intuition\n Describe your first thoughts on how to solve this problem. \nBreakdown the problem into simple steps and understand the example input clearly\n\nIf 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBreakdown the problem into simple steps and understand the example input clearly\n\n**If you learned something from solution. Please Upvote and subscribe to my youtube channel**\n\n*Thank you* \uD83D\uDE00\n\n# Easy Video Explanation\n\nhttps://youtu.be/2o-AmKvRSgw\n\n\n\n\n\n# Brute Force Solution\n\n```C++ []\nclass Solution {\npublic:\n    int countStudents(vector<int>& students, vector<int>& sandwiches) {\n        int len = students.size();\n        queue<int> studentQueue;\n        for(auto i:students)\n            studentQueue.push(i);\n        stack<int> sandwichStack;\n        for(int i=len-1;i>=0;i--) \n            sandwichStack.push(sandwiches[i]);\n        int served=0;\n        while(!studentQueue.empty() && served < studentQueue.size()) {\n            if (sandwichStack.top() == studentQueue.front()) {\n                sandwichStack.pop();\n                studentQueue.pop(); \n                served = 0;\n            } else {\n                studentQueue.push(studentQueue.front());\n                studentQueue.pop();\n                served++;\n            }\n        }\n        return studentQueue.size();\n    }\n};\n```\n```python []\nfrom collections import deque\n\nclass Solution:\n    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n        len_students = len(students)\n        student_queue = deque(students)\n        sandwich_stack = []\n        for sandwich in reversed(sandwiches):\n            sandwich_stack.append(sandwich)\n        served = 0\n        while student_queue and served < len(student_queue):\n            if sandwich_stack[-1] == student_queue[0]:\n                sandwich_stack.pop()\n                student_queue.popleft()\n                served = 0\n            else:\n                student_queue.append(student_queue[0])\n                student_queue.popleft()\n                served += 1\n        return len(student_queue)\n\n```\n```Java []\nclass Solution {\n    public int countStudents(int[] students, int[] sandwiches) {\n        int len = students.length;\n        Queue<Integer> studentQueue = new LinkedList<>();\n        for (int student : students)\n            studentQueue.add(student);\n        Stack<Integer> sandwichStack = new Stack<>();\n        for (int i = len - 1; i >= 0; i--)\n            sandwichStack.push(sandwiches[i]);\n        int served = 0;\n        while (!studentQueue.isEmpty() && served < studentQueue.size()) {\n            if (sandwichStack.peek().equals(studentQueue.peek())) {\n                sandwichStack.pop();\n                studentQueue.poll();\n                served = 0;\n            } else {\n                studentQueue.add(studentQueue.peek());\n                studentQueue.poll();\n                served++;\n            }\n        }\n        return studentQueue.size();\n    }\n}\n\n```\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    O(N*N)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(N)\n\n```C++ []\nclass Solution {\npublic:\n    int countStudents(vector<int>& students, vector<int>& sandwiches) {\n        int circleStudentCount=0,squareStudentCount=0;\n        for(auto i:students) {\n            if(i==1)\n                squareStudentCount++;\n            else\n                circleStudentCount++;\n        }\n        for(auto i:sandwiches) {\n            if(i==1) {\n                if(squareStudentCount>0)\n                    squareStudentCount--;\n                else\n                    break;\n            }\n            if(i==0) {\n                if(circleStudentCount>0)\n                    circleStudentCount--;\n                else\n                    break;\n            }\n        }\n        return squareStudentCount+circleStudentCount;\n    }\n};\n```\n```python []\nclass Solution:\n    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n        circle_student_count = 0\n        square_student_count = 0\n        for student in students:\n            if student == 1:\n                square_student_count += 1\n            else:\n                circle_student_count += 1\n        for sandwich in sandwiches:\n            if sandwich == 1:\n                if square_student_count > 0:\n                    square_student_count -= 1\n                else:\n                    break\n            if sandwich == 0:\n                if circle_student_count > 0:\n                    circle_student_count -= 1\n                else:\n                    break\n        return square_student_count + circle_student_count\n\n```\n```Java []\nimport java.util.*;\n\nclass Solution {\n    public int countStudents(int[] students, int[] sandwiches) {\n        int circleStudentCount = 0, squareStudentCount = 0;\n        for (int student : students) {\n            if (student == 1)\n                squareStudentCount++;\n            else\n                circleStudentCount++;\n        }\n        for (int sandwich : sandwiches) {\n            if (sandwich == 1) {\n                if (squareStudentCount > 0)\n                    squareStudentCount--;\n                else\n                    break;\n            }\n            if (sandwich == 0) {\n                if (circleStudentCount > 0)\n                    circleStudentCount--;\n                else\n                    break;\n            }\n        }\n        return squareStudentCount + circleStudentCount;\n    }\n}\n\n\n```\n# Optimal Solution\n```\nclass Solution {\npublic:\n    int countStudents(vector<int>& students, vector<int>& sandwiches) {\n        int circleStudentCount=0,squareStudentCount=0;\n        for(auto i:students) {\n            if(i==1)\n                squareStudentCount++;\n            else\n                circleStudentCount++;\n        }\n        for(auto i:sandwiches) {\n            if(i==1) {\n                if(squareStudentCount>0)\n                    squareStudentCount--;\n                else\n                    break;\n            }\n            if(i==0) {\n                if(circleStudentCount>0)\n                    circleStudentCount--;\n                else\n                    break;\n            }\n        }\n        return squareStudentCount+circleStudentCount;\n    }\n};\n```\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    O(N)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(1)\n\n\n# ***If you like the solution Please Upvote and subscribe to my youtube channel***\n***It Motivates me to record more videos***\n\n*Thank you* \uD83D\uDE00\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(1)
['Stack', 'Queue', 'Counting', 'Python', 'C++', 'Java']