forked from realpacific/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertInterval.kt
198 lines (162 loc) · 6.71 KB
/
InsertInterval.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package questions
import _utils.UseCommentAsDocumentation
import utils.assertIterableSame
import java.util.*
/**
* You are given an array of non-overlapping intervals `intervals` where `intervals[i] = [starti, endi]`
* represent the start and the end of the ith interval and intervals is sorted in ascending order by `starti`.
* You are also given an interval `newInterval = [start, end]` that represents the start and end of another interval.
*
* Insert `newInterval` into `intervals` such that `intervals` is still sorted in ascending order by `starti` and
* `intervals` still does not have any overlapping intervals (merge overlapping intervals if necessary).
* Return `intervals` after the insertion.
*
* [Source](https://leetcode.com/problems/insert-interval/)
*/
@UseCommentAsDocumentation
private fun insert(intervals: Array<IntArray>, newInterval: IntArray): Array<IntArray> {
if (intervals.isEmpty()) {
return arrayOf(newInterval)
}
fun hasOverlap(current: IntArray, intervalInHand: IntArray): Boolean {
val range1 = current.first()..current.last()
val range2 = intervalInHand.first()..intervalInHand.last()
return intervalInHand[0] in range1 || intervalInHand[1] in range1 || current[0] in range2 || current[1] in range2
}
fun isSelfInBetweenPrevAndCurrent(prev: IntArray, current: IntArray, self: IntArray): Boolean {
return self.first() > prev.last() && self.last() < current.first()
}
fun isLessThanFirst(): Boolean {
return newInterval.last() < intervals.first().last()
}
fun isHigherThanLast(): Boolean {
return newInterval.first() > intervals.last().last()
}
fun merge(vararg _intervals: IntArray): IntArray {
return intArrayOf(_intervals.map { it[0] }.minOrNull()!!, _intervals.map { it[1] }.maxOrNull()!!)
}
val result = arrayListOf<IntArray>()
fun getLatestAddedElem(): IntArray {
return result.last()
}
// check if whether [newInterval] should be inserted before [intervals] i.e. no overlaps with the first
if (isLessThanFirst() && !hasOverlap(intervals.first(), newInterval)) {
result.add(newInterval)
result.addAll(intervals)
return result.toTypedArray()
}
// check if whether [newInterval] should be inserted after [intervals] i.e. no overlaps with the last
if (isHigherThanLast() && !hasOverlap(intervals.last(), newInterval)) {
result.addAll(intervals)
result.add(newInterval)
return result.toTypedArray()
}
// add first interval immediately
// the latest value of [result] is what we will be merging with [newInterval]
result.add(intervals.first())
// loop till the size not the index as it might be required to merge [newIntervals] with the last element
for (i in 1..intervals.size) {
// [newInterval] has overlap with [result]'s latest so merge them and replace
if (hasOverlap(getLatestAddedElem(), newInterval)) {
val latestAddedInterval = result.removeAt(result.lastIndex) // remove
result.add(merge(latestAddedInterval, newInterval)) // merge and add
}
// now do same for the [current]
val current = intervals.getOrNull(i)
if (current != null) { // since we are looping till size
if (hasOverlap(current, getLatestAddedElem())) {
// [current] overlaps with the [result] so merge them
val latestAddedInterval = result.removeAt(result.lastIndex)
result.add(merge(latestAddedInterval, current))
} else {
// [current] doesn't require merging so, it should be added to [result]
// However, it is possible that the [newInterval] lies in between [current] & [result] WITHOUT overlaps
//
// In this case, the [newInterval] is added as it is to the final [result] and then [current] is added
if (isSelfInBetweenPrevAndCurrent(prev = getLatestAddedElem(), current = current, self = newInterval)) {
result.add(newInterval)
}
result.add(current)
}
}
}
return result.toTypedArray()
}
fun main() {
assertIterableSame(
actual = insert(
intervals = arrayOf(intArrayOf(3, 5), intArrayOf(12, 15)),
newInterval = intArrayOf(6, 6)
).toList(),
expected = arrayOf(intArrayOf(3, 5), intArrayOf(6, 6), intArrayOf(12, 15)).toList()
)
assertIterableSame(
actual = insert(
intervals = arrayOf(intArrayOf(1, 2)),
newInterval = intArrayOf(2, 7)
).toList(),
expected = arrayOf(intArrayOf(1, 7)).toList()
)
assertIterableSame(
actual = insert(
intervals = arrayOf(intArrayOf(1, 5)),
newInterval = intArrayOf(1, 7)
).toList(),
expected = arrayOf(intArrayOf(1, 7)).toList()
)
assertIterableSame(
actual = insert(
intervals = arrayOf(intArrayOf(1, 5)),
newInterval = intArrayOf(2, 7)
).toList(),
expected = arrayOf(intArrayOf(1, 7)).toList()
)
assertIterableSame(
actual = insert(
intervals = arrayOf(intArrayOf(1, 3), intArrayOf(6, 9)),
newInterval = intArrayOf(2, 5)
).toList(),
expected = arrayOf(intArrayOf(1, 5), intArrayOf(6, 9)).toList()
)
assertIterableSame(
actual = insert(
intervals = arrayOf(
intArrayOf(1, 2),
intArrayOf(3, 5),
intArrayOf(6, 7),
intArrayOf(8, 10),
intArrayOf(12, 16)
),
newInterval = intArrayOf(4, 8)
).toList(),
expected = arrayOf(intArrayOf(1, 2), intArrayOf(3, 10), intArrayOf(12, 16)).toList()
)
assertIterableSame(
actual = insert(
intervals = arrayOf(intArrayOf(3, 5)),
newInterval = intArrayOf(1, 2)
).toList(),
expected = arrayOf(intArrayOf(1, 2), intArrayOf(3, 5)).toList()
)
assertIterableSame(
actual = insert(
intervals = arrayOf(intArrayOf(3, 5)),
newInterval = intArrayOf(3, 5)
).toList(),
expected = arrayOf(intArrayOf(3, 5)).toList()
)
assertIterableSame(
actual = insert(
intervals = arrayOf(intArrayOf(1, 5)),
newInterval = intArrayOf(2, 3)
).toList(),
expected = arrayOf(intArrayOf(1, 5)).toList()
)
assertIterableSame(
actual = insert(
intervals = arrayOf(intArrayOf(1, 5)),
newInterval = intArrayOf(6, 8)
).toList(),
expected = arrayOf(intArrayOf(1, 5), intArrayOf(6, 8)).toList()
)
}