Easy Solution with Explanation 🔥🔥🔥 | Python3 🔥🔥🔥 | Stack | Python | Queue | 2 Approches
Intuition\nImagine you have two lines of students, each wanting either a circle-shaped sandwich or a square-shaped one. You have a magic plate that gives out sa
# Intuition\nImagine you have two lines of students, each wanting either a circle-shaped sandwich or a square-shaped one. You have a magic plate that gives out sandwiches, one by one, always the shape on top of the stack. If the student at the front of the line likes the sandwich shape that comes out, they eat it and leave the line. If they don\'t like it, they go to the back of the line, waiting for their preferred shape. We keep doing this until we reach a point where the sandwich shape nobody likes is on the top, and nobody can eat anymore, or everyone gets their favorite sandwich and the line is empty.\n\n\n# Approach\nWe\'ll count how many students like each shape of sandwich using a simple list, preferences, where preferences[0] is for circle sandwiches and preferences[1] for square sandwiches. Then, we go through each sandwich at the top of the stack. If at least one student likes this shape, we serve it (reduce the count of students waiting for this shape by one) and move on to the next sandwich. If no one likes the shape on top, we stop and count how many students are left without their preferred sandwich. If we manage to serve all the sandwiches, it means all students are happy and no one is left hungry.\n\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n2 Solutions - Brute force with O(n^2) is also mentioned belowpyth\n\n# Code\n```\nfrom collections import deque\n\nclass Solution:\n    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n        # Count the number of students preferring each type of sandwich\n        preferences = [0, 0]  # Index 0 for circular sandwiches, index 1 for square sandwiches\n        for preference in students:\n            preferences[preference] += 1\n        \n        # Iterate through sandwiches\n        for sandwich in sandwiches:\n            # If there are no more students left who prefer this type of sandwich, return the count\n            if preferences[sandwich] == 0:\n                return sum(preferences)\n            \n            # Serve the student if their preferred sandwich matches the current one\n            preferences[sandwich] -= 1\n        \n        # If the loop completes without returning, it means all students got their sandwiches\n        return 0\n\n# Upvote highly appreciated\n\n```\n\n\n```\nclass Solution:\n    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n        # Initialize a counter to track the count of non matching sandwich\n        count = 0\n        \n        # Keep iterating until there are no more students\n        while len(students) != 0:\n            # Check if the student at the front of the line likes the current sandwich\n            if students[0] == sandwiches[0]:\n                # If so, remove the student and the sandwich from their respective lists\n                students.pop(0)\n                sandwiches.pop(0)\n                # Reset the count since a match was found\n                count = 0\n            else:\n                # If not, increment the count and move the student to the end of the line\n                count += 1\n                students.append(students.pop(0))\n            \n            # If the count equals the number of remaining students, it means all students left in the line don\'t like the available sandwiches\n            if count == len(students):\n                return count\n        \n        # If all students have been served sandwiches, return 0 (indicating all students ate)\n        return 0\n\n# Upvote highly appreciated\n\n```
['Array', 'Stack', 'Queue', 'Simulation', 'Python', 'Python3']