File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed
non-overlapping-intervals Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .Arrays ;
2
+ import java .util .Comparator ;
3
+
4
+ class Solution {
5
+ public int eraseOverlapIntervals (int [][] intervals ) {
6
+ // ํ์ด: ์ ๋ ฌ ํ ๊ฐ ๊ตฌ๊ฐ์ ๋ ๊ฐ์ ์ ์ฅํด๊ฐ๋ฉฐ ์ ์์ ๋น๊ต, ์ ๊ฑฐ ๋์์ผ๋ ์นด์ดํธ๋ฅผ ์ฆ๊ฐ์์ผ ๋ฐํํ๋ค.
7
+ // TC: O(N)
8
+ // SC: O(1)
9
+
10
+ // intervals๋ฅผ ๊ฐ ๊ตฌ๊ฐ์ ๋ ๊ฐ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ
11
+ Arrays .sort (intervals , Comparator .comparingInt (a -> a [1 ]));
12
+
13
+ int answer = 0 ;
14
+ int end = intervals [0 ][1 ]; // ์ฒซ ๋ฒ์งธ ๊ตฌ๊ฐ์ ๋ ๊ฐ
15
+
16
+ // ๋ ๋ฒ์งธ ๊ตฌ๊ฐ๋ถํฐ ์ํํ๋ฉฐ ๊ฒน์น๋์ง ํ์ธ
17
+ for (int i = 1 ; i < intervals .length ; i ++) {
18
+ if (intervals [i ][0 ] < end ) { // ํ์ฌ ๊ตฌ๊ฐ์ด ์ด์ ๊ตฌ๊ฐ๊ณผ ๊ฒน์น๋ฉด
19
+ answer ++; // ์ ๊ฑฐ ํ์๋ฅผ ์ฆ๊ฐ
20
+ } else {
21
+ end = intervals [i ][1 ]; // ๊ฒน์น์ง ์์ผ๋ฉด ํ์ฌ ๊ตฌ๊ฐ์ ๋ ๊ฐ์ ๊ฐฑ์
22
+ }
23
+ }
24
+
25
+ return answer ;
26
+ }
27
+ }
You canโt perform that action at this time.
0 commit comments