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
+ ๊ตฌ๊ฐ์ด ๊ฒน์ณ ์ญ์ ํด์ผ ๋๋ ์ต์ ๊ตฌ๊ฐ์ ์๋ฅผ ๋ฐํํด๋ผ
3
+ -> Greedy
4
+ -> '๊ฒน์น์ง ์๊ฒ ์ต๋ํ ๋ง์ ๊ตฌ๊ฐ์ ๋จ๊ธฐ๊ณ , ๋๋จธ์ง๋ฅผ ์ ๊ฑฐ'
5
+
6
+ ๋ฌธ์ ํ์ด
7
+ 1. ๋๋๋ ์๊ฐ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌ
8
+ 2. ์ด์ ๊ตฌ๊ฐ์ ๋๊ณผ ํ์ฌ ๊ตฌ๊ฐ์ ์์์ ๋น๊ต
9
+ 3. ๊ฒน์น๋ฉด ํ์ฌ ๊ตฌ๊ฐ์ ์ ๊ฑฐ
10
+
11
+ TC: O(n log n), ์ ๋ ฌ O(n log n) + ๋ชจ๋ interval ํ ๋ฒ์ฉ ์ํ O(n)
12
+ SC: O(1)
13
+ """
14
+
15
+ from typing import List
16
+
17
+ class Solution :
18
+ def eraseOverlapIntervals (self , intervals : List [List [int ]]) -> int :
19
+ # ๋๋๋ ์๊ฐ์ด ๋น ๋ฅธ ์์ผ๋ก ์ ๋ ฌ
20
+ # -> ์ผ์ฐ ๋๋๋ ๊ตฌ๊ฐ์ ์ ํํ๋ฉด, ์ดํ์ ๋ ๋ง์ ๊ตฌ๊ฐ์ ๋ฃ์ ์ ์๋ ์ฌ์ง๊ฐ ์ปค์ง!
21
+ intervals .sort (key = lambda x : x [1 ])
22
+
23
+ # ์ฒซ ๊ตฌ๊ฐ ์ ํ
24
+ prev_end = float ('-inf' ) # ์์ ๋ฌดํ๋๋ก ๋น๊ต์ ๊ธฐ์ค๊ฐ ๊ฐ์ฅ ์๊ฒ ์ค์ -> ์ฒซ ๋ฒ์งธ ๊ตฌ๊ฐ ๋ฌด์กฐ๊ฑด ์ ํ๋จ
25
+ count = 0
26
+
27
+ # ํ๋์ฉ ๊ฒ์ฌ
28
+ for start , end in intervals :
29
+ if start >= prev_end :
30
+ # ๊ฒน์น์ง ์์ -> ๊ทธ๋๋ก ์ ์ง
31
+ prev_end = end
32
+ else :
33
+ # ๊ฒน์นจ -> ํ๋ ์ ๊ฑฐ
34
+ count += 1
35
+
36
+ return count
You canโt perform that action at this time.
0 commit comments