diff --git a/1700.number-of-students-unable-to-eat-lunch.js b/1700.number-of-students-unable-to-eat-lunch.js new file mode 100644 index 00000000..a2408a20 --- /dev/null +++ b/1700.number-of-students-unable-to-eat-lunch.js @@ -0,0 +1,36 @@ +/* URL of this problem + * https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/description/ + * + * @param {number[]} students + * @param {number[]} sandwiches + * @return {number} + */ + +var countStudents = function(students, sandwiches) { + const StudentQueue = [...students]; + const SandwichStack = [...sandwiches]; + + while (StudentQueue.length > 0) { + // The length of the student queue at the beginning of loop + const StudentLen = StudentQueue.length; + + for (let i = 0; i < StudentQueue.length; i++) { + const Removed = StudentQueue.shift(); + + if (Removed === SandwichStack[0]) { + SandwichStack.shift(); + } else { + StudentQueue.push(Removed); + } + } + + // Break the loop when the length at the beginning and the end of loop are the same + if (StudentLen === StudentQueue.length) { + break; + } + } + + return StudentQueue.length; +}; + +module.exports = countStudents; \ No newline at end of file diff --git a/countStudents.test.js b/countStudents.test.js new file mode 100644 index 00000000..d118567d --- /dev/null +++ b/countStudents.test.js @@ -0,0 +1,17 @@ +const countStudents = require("./1700.number-of-students-unable-to-eat-lunch"); + +test("Return 0 if all the students are able to eat sandwiches along with their preferences", () => { + expect(countStudents([1,1,0,0], [0,1,0,1])).toBe(0); +}); + +test("Return the number of students that are unable to eat sandwiches", () => { + expect(countStudents([1,1,1,0,0,1], [1,0,0,0,1,1])).toBe(3); +}); + +test("Return 0 if the input stundents has no element", () => { + expect(countStudents([], [1,0,0,0,1,1])).toBe(0); +}); + +test("Return the length of the input stundents if the input sandwiches has no element", () => { + expect(countStudents([1,1,1,0,0,1], [])).toBe(6); +}); \ No newline at end of file