File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ Big O
3+ - N: 주어진 배열 intervals의 길이
4+ - Time complexity: O(NlogN)
5+ - intervals를 start의 오름차순으로 정렬 -> O(NlogN)
6+ - 반복문 -> O(N)
7+ - O(NlogN + N) = O(NlogN)
8+ - Space complexity: O(N)
9+ - 정답 배열의 크기 -> O(N)
10+ */
11+
12+ import "sort"
13+
14+ func merge (intervals [][]int ) [][]int {
15+ sort .Slice (intervals , func (i , j int ) bool {
16+ return intervals [i ][0 ] < intervals [j ][0 ]
17+ })
18+ res := make ([][]int , 0 )
19+ start := intervals [0 ][0 ]
20+ end := intervals [0 ][1 ]
21+ for i := 1 ; i < len (intervals ); i ++ {
22+ curr := intervals [i ]
23+ if end >= curr [0 ] {
24+ end = max (end , curr [1 ])
25+ } else {
26+ res = append (res , []int {start , end })
27+ start = curr [0 ]
28+ end = curr [1 ]
29+ }
30+ }
31+ res = append (res , []int {start , end })
32+ return res
33+ }
34+
35+ func max (a , b int ) int {
36+ if a > b {
37+ return a
38+ } else {
39+ return b
40+ }
41+ }
You can’t perform that action at this time.
0 commit comments