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
23 changes: 23 additions & 0 deletions 252.meeting-rooms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* URL of this problem
* https://leetcode.com/problems/meeting-rooms/description/
*
* @param {number[][]} intervals
* @return {boolean}
*/

const canAttendMeetings = (intervals) => {
// Sort intervals in a ascending order by its start time
const Sorted = [...intervals].sort((a, b) => a[0] - b[0]);

for (let i = 0; i < Sorted.length - 1; i++) {
const CurrEnd = Sorted[i][1];
const NextStart = Sorted[i + 1][0];

if (CurrEnd > NextStart) {
return false;
}
}
return true;
};

module.exports = canAttendMeetings;
29 changes: 29 additions & 0 deletions canAttendMeetings.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const canAttendMeetings = require("../jest-test/252.meeting-rooms");

test("Return a person can attend all meetings", () => {
expect(
canAttendMeetings([
[7, 10],
[2, 4],
])
).toBeTruthy();
});

test("Return false if any start time and end time overlap", () => {
expect(
canAttendMeetings([
[0, 30],
[5, 10],
[15, 20],
])
).toBeFalsy();
});

test("Return false if there is Infinty at the end of the interval in the array", () => {
expect(
canAttendMeetings([
[7, 10],
[2, Infinity],
])
).toBeFalsy();
});