diff --git a/252.meeting-rooms.js b/252.meeting-rooms.js new file mode 100644 index 00000000..21a495b3 --- /dev/null +++ b/252.meeting-rooms.js @@ -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; diff --git a/canAttendMeetings.test.js b/canAttendMeetings.test.js new file mode 100644 index 00000000..d1186ff6 --- /dev/null +++ b/canAttendMeetings.test.js @@ -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(); +});