Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 60e621f

Browse files
authored
Create Non_Overlapping_Intervals.py
1 parent 9590fd9 commit 60e621f

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Time: O(N)
2+
class Solution:
3+
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
4+
# Sort the intervals by the second value
5+
intervals.sort(key=lambda x: x[1])
6+
st = [intervals[0]]
7+
ans = 0
8+
for i in intervals[1:]:
9+
top = st[-1]
10+
if i[0] < top[1]: # If there is an overlap, then that should't be considered
11+
ans += 1
12+
else:
13+
# If there is no overlap, then that item should be considered. Add it to the stack
14+
st.append(i)
15+
16+
return ans

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /