🟢Beats 100.00% of users with Java, Easy Solution with Explanation
Keep growing \uD83D\uDE0A\n\n\n---\n\n# Complexity\n\n- Time complexity: O(n)\n\n- Space complexity: O(1)\n\n---\n\n# Problem\n\n## Real-world scenario\n\nImagi
##### Keep growing \uD83D\uDE0A\n![image.png](https://assets.leetcode.com/users/images/379d4441-15c3-4869-8fef-002e4577aa62_1712540154.984739.png)\n\n---\n\n# Complexity\n\n- Time complexity: O(n)\n\n- Space complexity: O(1)\n\n---\n\n# Problem\n\n## Real-world scenario\n\nImagine a busy food court in a university campus where students queue up to grab sandwiches during their lunch break. The food court offers two types of sandwiches: circular and square. Each student in the queue has their own preference - some prefer circular sandwiches while others prefer square ones. The sandwiches are placed in a stack behind the counter.\n\nAt each step, the student at the front of the queue checks the top sandwich on the stack. If it matches their preference, they take it and leave the queue. Otherwise, they go to the end of the queue without taking any sandwich. This process continues until all students in the queue have either taken a sandwich or have been unable to find one that matches their preference.\n\nThe number of students unable to eat represents those who couldn\'t find a sandwich matching their preference before the stack of sandwiches ran out.\n\nSo, in this real-world scenario, you\'re given the preferences of students (whether they prefer circular or square sandwiches) and the types of sandwiches available in the stack. Your task is to determine how many students in the queue were unable to eat because they couldn\'t find a sandwich that matched their preference before the stack of sandwiches ran out.\n\n---\n\n# Approach Explanation\n\n### Counting Students with Different Sandwich Preferences:\n- The function `countStudents` takes in two arrays: one representing the preferences of students (either 1 for circular sandwiches or 0 for square sandwiches) and the other representing the available sandwiches (again, 1 for circular and 0 for square).\n- It starts by initializing two counters: `studentsWithSquareSandwiches` and `studentsWithCircularSandwiches`. These variables will keep track of how many students prefer square sandwiches and how many prefer circular sandwiches, respectively.\n- It then goes through each student\'s preference array and increments the appropriate counter based on their sandwich preference.\n\n### Distributing Sandwiches to Students:\n- After counting the preferences, the code goes through each sandwich in the provided sandwiches array.\n- If the sandwich is circular (`sandwich == 1`) and there are still students who prefer circular sandwiches (`studentsWithCircularSandwiches > 0`), it gives a circular sandwich to a student by decrementing `studentsWithCircularSandwiches`.\n- Similarly, if the sandwich is square (`sandwich == 0`) and there are still students who prefer square sandwiches (`studentsWithSquareSandwiches > 0`), it gives a square sandwich to a student by decrementing `studentsWithSquareSandwiches`.\n- If at any point there are no more students who prefer the type of sandwich available, the loop breaks and the function returns the remaining count of students who have not received their preferred sandwiches.\n\n### Returning the Result:\n- If the loop completes without breaking, it means all students have received their preferred sandwiches, so it returns `0` indicating no remaining students.\n- If the loop breaks prematurely due to insufficient sandwiches, it returns the count of remaining students who haven\'t received their sandwiches.\n\n---\n\n# Pseudocode\n\n```Javascript []\nStart\nInitialize \'studentsWithSquareSandwiches\' to 0\nInitialize \'studentsWithCircularSandwiches\' to 0\n\nIterate through each \'student\' in \'students\'\n    If \'student\' is equal to 1, increment \'studentsWithCircularSandwiches\'\n    Else, increment \'studentsWithSquareSandwiches\'\nEnd\n\nIterate through each \'sandwich\' in \'sandwiches\'\n    If \'sandwich\' is equal to 1 and \'studentsWithCircularSandwiches\' is greater than 0\n        Decrement \'studentsWithCircularSandwiches\'\n    Else if \'sandwich\' is equal to 0 and \'studentsWithSquareSandwiches\' is greater than 0\n        Decrement \'studentsWithSquareSandwiches\'\n    Else\n        Return the sum of \'studentsWithCircularSandwiches\' and \'studentsWithSquareSandwiches\'\nEnd\n\nReturn 0\nEnd\n```\n\n# Code\n\n```java []\nclass Solution {\n\n    public int countStudents(int[] students, int[] sandwiches) {\n\n        int studentsWithSquareSandwiches = 0, studentsWithCircularSandwiches = 0;\n\n        for (int student : students) {\n            if (student == 1) {\n                studentsWithCircularSandwiches++;\n            } else {\n                studentsWithSquareSandwiches++;\n            }\n        }\n\n        for (int sandwich : sandwiches) {\n            if (sandwich == 1 && studentsWithCircularSandwiches > 0) {\n                studentsWithCircularSandwiches--;\n            } else if (sandwich == 0 && studentsWithSquareSandwiches > 0) {\n                studentsWithSquareSandwiches--;\n            } else {\n                return studentsWithCircularSandwiches + studentsWithSquareSandwiches;\n            }\n        }\n\n        return 0;\n    }\n}\n```\n----\n> ![VOTE](https://assets.leetcode.com/users/images/8bc25f2b-9256-46ad-a27c-2746a7c529bc_1712670904.6992257.gif)\n\n# *If you have any question, feel free to ask. If you found my solution helpful, Please UPVOTE !* \uD83D\uDE0A
['Array', 'Stack', 'Queue', 'Simulation', 'Java']