Skip to content

Commit 29b1b77

Browse files
committed
Added Meeting Rooms II to Intervals
1 parent 2b3589a commit 29b1b77

File tree

2 files changed

+178
-2
lines changed

2 files changed

+178
-2
lines changed

.ipynb_checkpoints/Intervals-checkpoint.ipynb

+89-1
Original file line numberDiff line numberDiff line change
@@ -371,10 +371,98 @@
371371
"};"
372372
]
373373
},
374+
{
375+
"cell_type": "markdown",
376+
"id": "24c37b4d",
377+
"metadata": {},
378+
"source": [
379+
"## Meeting Rooms II\n",
380+
"\n",
381+
"* https://www.lintcode.com/problem/919/description\n",
382+
"***\n",
383+
"* Time Complexity: O(nlogn)\n",
384+
" - traverse through all intervals and push the start/end times into an arr: O(n)\n",
385+
" - sort the meetings arr: O(nlogn)\n",
386+
" * the # of entries in the meetings arr is actually 2n b/c we push both start and end times separately\n",
387+
"* Space Complexity: O(n)\n",
388+
" - needs space for the meetings arr\n",
389+
"***\n",
390+
"* similar to Kadane's algorithm\n",
391+
"* we traverse through the intervals arr and push the start times and end times for each interval separately\n",
392+
" - [interval.start, 1] and [interval.end, -1]\n",
393+
" - a start time indicates a meeting starting and an end time indicates a meeting ending\n",
394+
" - so the 1 and -1 respectively will adjust the # of meetings going on at a time, which corresponds to the # of conference rooms\n",
395+
"* once we have filled up the meetings arr, we sort it\n",
396+
" - we do this b/c we want to determine how many concurrent meetings, and therefore conference rooms being used, there are\n",
397+
" - so if we have 2 start times in a row, that tells us that there are 2 meetings happening at the same time, which require 2 conference rooms\n",
398+
" - however, if the next time is an end time, then we know that a meeting has ended and the # of meetings and conference rooms have gone down\n",
399+
"* as we traverse meetings, the # of meetings/conference rooms will reach a peak then go down to 0 b/c all meetings have ended by the time we have traversed the entire meetings arr\n",
400+
" - therefore, we must take the max # of meetings after every loop to ensure we get our # of conference rooms"
401+
]
402+
},
403+
{
404+
"cell_type": "code",
405+
"execution_count": 1,
406+
"id": "493fe650",
407+
"metadata": {},
408+
"outputs": [],
409+
"source": [
410+
"/**\n",
411+
" * Definition of Interval:\n",
412+
" * class Interval {\n",
413+
" * constructor(start, end) {\n",
414+
" * this.start = start;\n",
415+
" * this.end = end;\n",
416+
" * }\n",
417+
" * }\n",
418+
" */\n",
419+
"\n",
420+
"var Solution = class {\n",
421+
" /**\n",
422+
" * @param intervals: an array of meeting time intervals\n",
423+
" * @return: the minimum number of conference rooms required\n",
424+
" */\n",
425+
" minMeetingRooms(intervals) {\n",
426+
" const n = intervals.length;\n",
427+
" if (n === 1) return 1;\n",
428+
"\n",
429+
" // essentially, every time a meeting starts, we increment # of meetings by 1\n",
430+
" // and every time a meeting ends, we decrement # of meetings by 1\n",
431+
" // so we keep track of these in an arr\n",
432+
" const meetings = [];\n",
433+
" intervals.forEach(interval => {\n",
434+
" meetings.push([interval.start, 1]);\n",
435+
" meetings.push([interval.end, -1]);\n",
436+
" })\n",
437+
"\n",
438+
" // we then sort the meetings arr by their time\n",
439+
" meetings.sort((a, b) => a[0] - b[0]);\n",
440+
"\n",
441+
" // pretty similar to Kadane's algorithm\n",
442+
" // we aren't looking for overlaps here!\n",
443+
" // we know that a meeting HAS to start and it HAS to end\n",
444+
" // if a meeting starts and then another meeting starts after it with the previous one not ending yet,\n",
445+
" // then we'll need 2 conference rooms\n",
446+
" // but if one of them ends, then we only have need for 1 conference room\n",
447+
" \n",
448+
" // the reason why we need to take the max every time is b/c by the end of all meetings, it'll be back to 0\n",
449+
" // so we must look at the max concurrent meetings\n",
450+
" let maxMeetings = 0;\n",
451+
" let runningMeetings = 0;\n",
452+
" for (let [_, delta] of meetings) {\n",
453+
" runningMeetings += delta;\n",
454+
" maxMeetings = Math.max(maxMeetings, runningMeetings);\n",
455+
" }\n",
456+
"\n",
457+
" return maxMeetings;\n",
458+
" }\n",
459+
"}"
460+
]
461+
},
374462
{
375463
"cell_type": "code",
376464
"execution_count": null,
377-
"id": "57fccd86",
465+
"id": "4bd16fdb",
378466
"metadata": {},
379467
"outputs": [],
380468
"source": []

Intervals.ipynb

+89-1
Original file line numberDiff line numberDiff line change
@@ -371,10 +371,98 @@
371371
"};"
372372
]
373373
},
374+
{
375+
"cell_type": "markdown",
376+
"id": "24c37b4d",
377+
"metadata": {},
378+
"source": [
379+
"## Meeting Rooms II\n",
380+
"\n",
381+
"* https://www.lintcode.com/problem/919/description\n",
382+
"***\n",
383+
"* Time Complexity: O(nlogn)\n",
384+
" - traverse through all intervals and push the start/end times into an arr: O(n)\n",
385+
" - sort the meetings arr: O(nlogn)\n",
386+
" * the # of entries in the meetings arr is actually 2n b/c we push both start and end times separately\n",
387+
"* Space Complexity: O(n)\n",
388+
" - needs space for the meetings arr\n",
389+
"***\n",
390+
"* similar to Kadane's algorithm\n",
391+
"* we traverse through the intervals arr and push the start times and end times for each interval separately\n",
392+
" - [interval.start, 1] and [interval.end, -1]\n",
393+
" - a start time indicates a meeting starting and an end time indicates a meeting ending\n",
394+
" - so the 1 and -1 respectively will adjust the # of meetings going on at a time, which corresponds to the # of conference rooms\n",
395+
"* once we have filled up the meetings arr, we sort it\n",
396+
" - we do this b/c we want to determine how many concurrent meetings, and therefore conference rooms being used, there are\n",
397+
" - so if we have 2 start times in a row, that tells us that there are 2 meetings happening at the same time, which require 2 conference rooms\n",
398+
" - however, if the next time is an end time, then we know that a meeting has ended and the # of meetings and conference rooms have gone down\n",
399+
"* as we traverse meetings, the # of meetings/conference rooms will reach a peak then go down to 0 b/c all meetings have ended by the time we have traversed the entire meetings arr\n",
400+
" - therefore, we must take the max # of meetings after every loop to ensure we get our # of conference rooms"
401+
]
402+
},
403+
{
404+
"cell_type": "code",
405+
"execution_count": 1,
406+
"id": "493fe650",
407+
"metadata": {},
408+
"outputs": [],
409+
"source": [
410+
"/**\n",
411+
" * Definition of Interval:\n",
412+
" * class Interval {\n",
413+
" * constructor(start, end) {\n",
414+
" * this.start = start;\n",
415+
" * this.end = end;\n",
416+
" * }\n",
417+
" * }\n",
418+
" */\n",
419+
"\n",
420+
"var Solution = class {\n",
421+
" /**\n",
422+
" * @param intervals: an array of meeting time intervals\n",
423+
" * @return: the minimum number of conference rooms required\n",
424+
" */\n",
425+
" minMeetingRooms(intervals) {\n",
426+
" const n = intervals.length;\n",
427+
" if (n === 1) return 1;\n",
428+
"\n",
429+
" // essentially, every time a meeting starts, we increment # of meetings by 1\n",
430+
" // and every time a meeting ends, we decrement # of meetings by 1\n",
431+
" // so we keep track of these in an arr\n",
432+
" const meetings = [];\n",
433+
" intervals.forEach(interval => {\n",
434+
" meetings.push([interval.start, 1]);\n",
435+
" meetings.push([interval.end, -1]);\n",
436+
" })\n",
437+
"\n",
438+
" // we then sort the meetings arr by their time\n",
439+
" meetings.sort((a, b) => a[0] - b[0]);\n",
440+
"\n",
441+
" // pretty similar to Kadane's algorithm\n",
442+
" // we aren't looking for overlaps here!\n",
443+
" // we know that a meeting HAS to start and it HAS to end\n",
444+
" // if a meeting starts and then another meeting starts after it with the previous one not ending yet,\n",
445+
" // then we'll need 2 conference rooms\n",
446+
" // but if one of them ends, then we only have need for 1 conference room\n",
447+
" \n",
448+
" // the reason why we need to take the max every time is b/c by the end of all meetings, it'll be back to 0\n",
449+
" // so we must look at the max concurrent meetings\n",
450+
" let maxMeetings = 0;\n",
451+
" let runningMeetings = 0;\n",
452+
" for (let [_, delta] of meetings) {\n",
453+
" runningMeetings += delta;\n",
454+
" maxMeetings = Math.max(maxMeetings, runningMeetings);\n",
455+
" }\n",
456+
"\n",
457+
" return maxMeetings;\n",
458+
" }\n",
459+
"}"
460+
]
461+
},
374462
{
375463
"cell_type": "code",
376464
"execution_count": null,
377-
"id": "57fccd86",
465+
"id": "4bd16fdb",
378466
"metadata": {},
379467
"outputs": [],
380468
"source": []

0 commit comments

Comments
 (0)