File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
non-overlapping-intervals Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ Constraints:
3+ - 1 <= intervals.length <= 10^5
4+ - intervals[i].length == 2
5+ - -5 * 10^4 <= starti < endi <= 5 * 10^4
6+
7+ Time Complexity: O(n * log n)
8+ - ๊ตฌ๊ฐ ์ ๋ ฌ ์ O(n * log n) ์ฌ์ฉ, ์ ๋ ฌ๋ ๊ตฌ๊ฐ์ ์ํํ ๋ O(n)
9+
10+ Space Complexity: O(1)
11+ - ์ ํด์ง ๋ณ์ ์ธ์๋ ๊ณต๊ฐ ์ฌ์ฉํ์ง ์์
12+
13+ ํ์ด๋ฐฉ๋ฒ:
14+ 1. ๋์ ์ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌ <- ์ ๊ฑฐํ ๊ตฌ๊ฐ์ ์๋ฅผ ์ต์ํํ๊ธฐ ์ํด
15+ 2. ์ฒซ ๋ฒ์งธ ๊ตฌ๊ฐ์ ์ ํํ๊ณ ๊ทธ ๊ตฌ๊ฐ์ ๋์ ์ ๊ธฐ๋กํจ
16+ 3. ๋ ๋ฒ์งธ ๊ตฌ๊ฐ๋ถํฐ end๋ณด๋ค ์์์ ์ด ํฌ๊ฑฐ๋ ๊ฐ์(๊ฒน์น์ง ์๋) ๊ตฌ๊ฐ์ ์ฐพ์์ ์นด์ดํธ
17+ 4. ์ ์ฒด ๊ตฌ๊ฐ์ ์์์ count๋ฅผ ๋นผ์ ์ ๊ฑฐํด์ผ ํ ๊ตฌ๊ฐ์ ์๋ฅผ ๋ฐํ
18+ """
19+ class Solution :
20+ def eraseOverlapIntervals (self , intervals : List [List [int ]]) -> int :
21+ if not intervals :
22+ return 0
23+
24+ intervals .sort (key = lambda x : x [1 ])
25+
26+ count = 1
27+ end = intervals [0 ][1 ]
28+
29+ for interval in intervals [1 :]:
30+
31+ if interval [0 ] >= end :
32+ count += 1
33+ end = interval [1 ]
34+
35+ return len (intervals ) - count
36+
You canโt perform that action at this time.
0 commit comments