Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions 1700.number-of-students-unable-to-eat-lunch.js
Original file line number Diff line number Diff line change
@@ -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;
17 changes: 17 additions & 0 deletions countStudents.test.js
Original file line number Diff line number Diff line change
@@ -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);
});