Skip to content

Commit 12bc99d

Browse files
committed
435. Non-overlapping Intervals Solution
1 parent 284ce27 commit 12bc99d

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

insert-interval/doh6077.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Solution:
2+
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
3+
4+
# # first structure it as graph (dictionary)
5+
# intervals = [[1,2],[2,3],[3,4],[1,3]]
6+
# dict = {}
7+
# for i in intervals:
8+
# dict[i[0]] = []
9+
# for a,b in intervals:
10+
# dict[a].append(b)
11+
12+
# #Overlapping edge cases
13+
# # 1. [1,4], [2,3] -> not sure how to figure out
14+
# # 2. [1,3], [1,2] , [3,4] -> check if the value has connection
15+
# # 3. [1,2], [1,2], [1,2] ( same values) use hashset
16+
17+
# count = 0
18+
# visited = set()
19+
# for key, value in dict.items():
20+
# if value not in visited:
21+
# visited.append(value)
22+
# else:
23+
# count += 1
24+
# return count
25+
26+
# count = 0
27+
# visited = set()
28+
# for a, b in intervals:
29+
# if b not in visited:
30+
# visited.add(b)
31+
# else:
32+
# count += 1
33+
# return count
34+
35+
36+
# greedy Approach
37+
intervals.sort()
38+
39+
res = 0
40+
prevEnd = intervals[0][1]
41+
for start, end in intervals[1:]:
42+
if start >= prevEnd:
43+
prevEnd = end
44+
else:
45+
res += 1
46+
prevEnd = min(prevEnd, end)
47+
return res
48+

0 commit comments

Comments
 (0)