Skip to content

Commit 2b3589a

Browse files
committed
Added Meeting Rooms to Intervals
1 parent 382cc95 commit 2b3589a

File tree

2 files changed

+148
-4
lines changed

2 files changed

+148
-4
lines changed

.ipynb_checkpoints/Intervals-checkpoint.ipynb

+74-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,77 @@
11
{
22
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "c379a5b5",
6+
"metadata": {},
7+
"source": [
8+
"# Easy"
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"id": "5bfb5f52",
14+
"metadata": {},
15+
"source": [
16+
"## Meeting Rooms\n",
17+
"\n",
18+
"* https://www.lintcode.com/problem/920/\n",
19+
"***\n",
20+
"* Time Complexity: O(nlogn)\n",
21+
" - must sort intervals arr by start time: O(nlogn)\n",
22+
" - then traverse through intervals arr and find any overlaps O(n)\n",
23+
"* Space Complexity: O(1)\n",
24+
" - only needs space for a couple of variables\n",
25+
"***\n",
26+
"* we sort intervals by start time so that it is easier to traverse the arr and determine if there is an overlap\n",
27+
" - we just see if int1[end] > int2[start]\n",
28+
" - if it is, we return false"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": 4,
34+
"id": "1c72f1a4",
35+
"metadata": {},
36+
"outputs": [],
37+
"source": [
38+
"/**\n",
39+
" * Definition of Interval:\n",
40+
" * class Interval {\n",
41+
" * constructor(start, end) {\n",
42+
" * this.start = start;\n",
43+
" * this.end = end;\n",
44+
" * }\n",
45+
" * }\n",
46+
" */\n",
47+
"\n",
48+
"var Solution = class {\n",
49+
" /**\n",
50+
" * @param intervals: an array of meeting time intervals\n",
51+
" * @return: if a person could attend all meetings\n",
52+
" */\n",
53+
" canAttendMeetings(intervals) {\n",
54+
" const n = intervals.length;\n",
55+
"\n",
56+
" if (n === 1) return true;\n",
57+
"\n",
58+
" // sort it by start times\n",
59+
" intervals.sort((a, b) => a.start - b.start);\n",
60+
"\n",
61+
" let newInterval = intervals[0];\n",
62+
" for (let i = 1; i < n; i++) {\n",
63+
" if (newInterval.end > intervals[i].start) {\n",
64+
" return false;\n",
65+
" }\n",
66+
"\n",
67+
" newInterval = intervals[i];\n",
68+
" }\n",
69+
"\n",
70+
" return true;\n",
71+
" }\n",
72+
"}"
73+
]
74+
},
375
{
476
"cell_type": "markdown",
577
"id": "e59a494c",
@@ -148,7 +220,7 @@
148220
},
149221
{
150222
"cell_type": "markdown",
151-
"id": "bf62193a",
223+
"id": "2f905e1f",
152224
"metadata": {},
153225
"source": [
154226
"## Merge Intervals\n",
@@ -234,7 +306,7 @@
234306
},
235307
{
236308
"cell_type": "markdown",
237-
"id": "6cbad747",
309+
"id": "76c99826",
238310
"metadata": {},
239311
"source": [
240312
"## Non-overlapping Intervals\n",

Intervals.ipynb

+74-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,77 @@
11
{
22
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "c379a5b5",
6+
"metadata": {},
7+
"source": [
8+
"# Easy"
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"id": "5bfb5f52",
14+
"metadata": {},
15+
"source": [
16+
"## Meeting Rooms\n",
17+
"\n",
18+
"* https://www.lintcode.com/problem/920/\n",
19+
"***\n",
20+
"* Time Complexity: O(nlogn)\n",
21+
" - must sort intervals arr by start time: O(nlogn)\n",
22+
" - then traverse through intervals arr and find any overlaps O(n)\n",
23+
"* Space Complexity: O(1)\n",
24+
" - only needs space for a couple of variables\n",
25+
"***\n",
26+
"* we sort intervals by start time so that it is easier to traverse the arr and determine if there is an overlap\n",
27+
" - we just see if int1[end] > int2[start]\n",
28+
" - if it is, we return false"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": 4,
34+
"id": "1c72f1a4",
35+
"metadata": {},
36+
"outputs": [],
37+
"source": [
38+
"/**\n",
39+
" * Definition of Interval:\n",
40+
" * class Interval {\n",
41+
" * constructor(start, end) {\n",
42+
" * this.start = start;\n",
43+
" * this.end = end;\n",
44+
" * }\n",
45+
" * }\n",
46+
" */\n",
47+
"\n",
48+
"var Solution = class {\n",
49+
" /**\n",
50+
" * @param intervals: an array of meeting time intervals\n",
51+
" * @return: if a person could attend all meetings\n",
52+
" */\n",
53+
" canAttendMeetings(intervals) {\n",
54+
" const n = intervals.length;\n",
55+
"\n",
56+
" if (n === 1) return true;\n",
57+
"\n",
58+
" // sort it by start times\n",
59+
" intervals.sort((a, b) => a.start - b.start);\n",
60+
"\n",
61+
" let newInterval = intervals[0];\n",
62+
" for (let i = 1; i < n; i++) {\n",
63+
" if (newInterval.end > intervals[i].start) {\n",
64+
" return false;\n",
65+
" }\n",
66+
"\n",
67+
" newInterval = intervals[i];\n",
68+
" }\n",
69+
"\n",
70+
" return true;\n",
71+
" }\n",
72+
"}"
73+
]
74+
},
375
{
476
"cell_type": "markdown",
577
"id": "e59a494c",
@@ -148,7 +220,7 @@
148220
},
149221
{
150222
"cell_type": "markdown",
151-
"id": "bf62193a",
223+
"id": "2f905e1f",
152224
"metadata": {},
153225
"source": [
154226
"## Merge Intervals\n",
@@ -234,7 +306,7 @@
234306
},
235307
{
236308
"cell_type": "markdown",
237-
"id": "6cbad747",
309+
"id": "76c99826",
238310
"metadata": {},
239311
"source": [
240312
"## Non-overlapping Intervals\n",

0 commit comments

Comments
 (0)