Skip to content

Commit 3800d3c

Browse files
author
이연수
committed
merge intervals
1 parent 694eba9 commit 3800d3c

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

merge-intervals/EcoFriendlyAppleSu.kt

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package leetcode_study
2+
3+
/*
4+
* 주어진 범위 값 병합 문제
5+
* */
6+
7+
/*
8+
* 첫 번째 풀이. 틀림
9+
* 범위 안에 속하는 모든 값을 Set 자료구조에 넣고 제일 큰 숫자 만큼의 Boolean Array를 만든 후 방문처리하는 방식으로 접근
10+
* 예외 케이스
11+
* intervals = [[1,4],[5,6]] 일 때,
12+
* 연속된 값으로 인식해 답을 [[1,6]]로 도출. 올바른 답은 [[1,4],[5,6]]
13+
* */
14+
fun merge01(intervals: Array<IntArray>): Array<IntArray> {
15+
val tempSet = mutableSetOf<Int>()
16+
var maxValue = Int.MIN_VALUE
17+
18+
for (interval in intervals) {
19+
val leftValue = interval[0]
20+
val rightValue = interval[1]
21+
maxValue = max(maxValue, rightValue)
22+
for (value in leftValue until rightValue + 1) {
23+
tempSet.add(value)
24+
}
25+
}
26+
27+
val checkVisited = BooleanArray(maxValue + 1) { false }
28+
for (value in tempSet) {
29+
checkVisited[value] = true
30+
}
31+
32+
val result = mutableListOf<IntArray>()
33+
var start: Int? = null
34+
35+
for (i in checkVisited.indices) {
36+
if (checkVisited[i]) {
37+
if (start == null) start = i // 시작점 저장
38+
// 연속된 값이 true일 경우엔 넘어감
39+
} else {
40+
if (start != null) {
41+
result.add(intArrayOf(start, i - 1)) // [start, end] 추가
42+
start = null
43+
}
44+
}
45+
}
46+
if (start != null) {
47+
result.add(intArrayOf(start, checkVisited.lastIndex))
48+
}
49+
50+
return result.toTypedArray()
51+
}
52+
53+
/*
54+
* 주어진 범위 값을 정렬하고 순회하면서 병합 여부 판단
55+
* 시간 복잡도: O(n log n)
56+
* -> 첫 번째 원소 기준으로 TimSort 알고리즘을 사용한 정렬: O(n log n)
57+
* -> 주어진 interval 만큼 순회해 계산: O(n)
58+
* 공간 복잡도: O(n)
59+
* -> 첫 번째 원소로 정렬된 sortedIntervals를 담는 공간: O(n)
60+
* */
61+
fun merge02(intervals: Array<IntArray>): Array<IntArray> {
62+
if (intervals.isEmpty()) return intervals
63+
64+
// 시작점 기준으로 정렬
65+
val sortedIntervals = intervals.sortedBy { it[0] }
66+
67+
val result = mutableListOf<IntArray>()
68+
var currentInterval = sortedIntervals[0]
69+
70+
for (i in 1 until sortedIntervals.size) {
71+
val interval = sortedIntervals[i]
72+
// 겹치는 경우: 현재 구간의 끝이 다음 구간의 시작보다 크거나 같으면 merge
73+
if (currentInterval[1] >= interval[0]) {
74+
currentInterval[1] = maxOf(currentInterval[1], interval[1])
75+
} else {
76+
// 겹치지 않으면 현재 구간을 결과에 추가하고, 새 구간으로 변경
77+
result.add(currentInterval)
78+
currentInterval = interval
79+
}
80+
}
81+
// 마지막 구간 추가
82+
result.add(currentInterval)
83+
84+
return result.toTypedArray()
85+
}

0 commit comments

Comments
 (0)