BEATS 100% OF USERS || Simple explanation with clean commented code
Intuition\nAllocate a student to each sandwich instead of allocating sandwiches to students.\n\n# Approach\n- count all the students who wants circular sandwich
# Intuition\nAllocate a student to each sandwich instead of allocating sandwiches to students.\n\n# Approach\n- count all the students who wants circular sandwiches and square sandwiches respectively\n- for each sandwich:\n\n        if there is any student who wants it:\n            allocate the sandwich to him/her and remove him/her\n        else\n            return the number of students left\n\n\n# Complexity\n- Time complexity:\nO(N)\nwhere N is the total number of students/sandwiches\n\n- Space complexity:\nO(1)\n\n# Code\n- C++:\n```\nclass Solution {\npublic:\n    int countStudents(vector<int>& students, vector<int>& sandwiches) {\n        int st1=accumulate(students.begin(),students.end(),0);\n        int st0=students.size()-st1;\n        for(auto x:sandwiches)\n        {\n            if(x)                   // square sandwich\n            {\n                if(st1)             // if there are students who want a square sandwich\n                st1--;              // allocate to one of the students and remove him\n\n                else                // if no student wants a square sandwich No other sandwich \n                return st0+st1;     // in the stack can be allocated without allocating this sandwich\n            }\n            else                    // circular sandwich\n            {\n                if(st0)             // If there are students who wants circular sandwich\n                st0--;              // allocate to him and remove him\n\n                else                // If no student wants a circular sandwich, No other sandwich\n                return st0+st1;     //in the stack can be allocated without allocating this sandwich\n\n            }\n        }\n        return 0;\n    }\n};\n```\n\n- Python:\n```\nclass Solution(object):\n    def countStudents(self, students, sandwiches):\n        st0 = students.count(0)  # Count number of 0s\n        st1 = len(students) - st0  # Remaining students are 1s\n\n        for x in sandwiches:\n            if x == 1:  # square sandwich\n                if st1 > 0:         #If there are students who want a square sandwich\n                    st1 -= 1        #allocate to him and remove him\n\n                else:               #If no student wants a square sandwich, No other sandwich \n                    return st0+st1  #in the stack can be allocated without allocating this sandwich\n                                    \n                    \n            else:       #circular sandwich\n                if st0 > 0:         # If there are students who wants circular sandwich\n                    st0 -= 1        # allocate to him and remove him\n\n                else:               # If no student wants a circular sandwich, No other sandwich \n                    return st0+st1  # in the stack can be allocated without allocating this sandwich\n\n        return 0  # All sandwiches distributed successfully\n```\n\n- Java:\n```\nclass Solution {\n    public int countStudents(int[] students, int[] sandwiches) {\n        int st0 = (int) Arrays.stream(students).filter(x -> x == 0).count(); \n        int st1 = students.length - st0; \n        \n        for (int x : sandwiches) {\n            if (x == 1) { \n                if (st1 > 0) st1--;  \n                else return st0+st1; \n            } else { \n                if (st0 > 0) st0--; \n                else return st1+st0; \n            }\n        }\n        \n        return 0; \n    }\n}\n```\n
['Python', 'C++', 'Java']