[Javascript] Queue v.s. Count
This problem could either done by simulation or counting.\n.\nSimulation with QUEUE\n\n\nvar countStudents = function(students, sandwiches) {\n    let count=0;\
This problem could either done by **simulation** or **counting**.\n.\nSimulation with **QUEUE**\n\n```\nvar countStudents = function(students, sandwiches) {\n    let count=0;\n\t// if count=students.length, means there\'s an infinite-loop\n    while(count<students.length && students.length>0){\n        if(students[0]==sandwiches[0]){\n            count=0;\n\t\t\t// CASE1: student gets a sandwich and leaves.\n\t\t\tstudents.shift(); sandwiches.shift();\n        }else{\n            count++;\n\t\t\t// CASE2: student goes to queue\'s end.\n\t\t\tstudents.push(students.shift());\n        }\n    }\n    return students.length;\n};\n```\n\n**Counting**\n\n```\nvar countStudents = function(students, sandwiches) {\n    let prefer=[0,0];\n    for(let i=0; i<students.length; i++){\n        prefer[students[i]]++;\n    }\n\t// each sandwich needs to find a student who wants it.\n    for(let j=0; j<sandwiches.length; j++){\n\t\t// if prefer[x]<0, means nobody wants this sandwich anymore (i.e. everyone in queue prefers another).\n        if(--prefer[sandwiches[j]]<0){\n\t\t\t// So they can\'t have lunch now :(\n            return prefer[1-sandwiches[j]];\n        }\n    }\n    return 0;\n};\n```\n\nThanks for your reading and **up-voting** :)\n\n**\u2B50 Check [HERE](https://github.com/Lynn19950915/LeetCode_King) for my full Leetcode Notes ~**\n\n
['Queue', 'JavaScript']