Skip to content

Commit 7ec6e1b

Browse files
authored
Merge pull request #10 from kotler-dev/exercice/id57
exercise/id57
2 parents 37f900a + c7fc5d2 commit 7ec6e1b

File tree

4 files changed

+277
-0
lines changed

4 files changed

+277
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ My Kotlin exercises with tests from [Leetcode](https://leetcode.com/kotlerdev)
5555
| 217 | [Contains Duplicate](src/main/kotlin/exercise/easy/id217/Description217.md) | [solution](src/main/kotlin/exercise/easy/id217/Solution217.kt) | [test](src/test/kotlin/exercise/easy/id217/Solution217Test.kt) | Easy |
5656
| 283 | [Move Zeroes](src/main/kotlin/exercise/easy/id283/Description283.md) | [solution](src/main/kotlin/exercise/easy/id283/Solution283.kt) | [test](src/test/kotlin/exercise/easy/id283/Solution283Test.kt) | Easy |
5757
| 977 | [Squares of a Sorted Array](src/main/kotlin/exercise/easy/id977/Description977.md) | [solution](src/main/kotlin/exercise/easy/id977/Solution977.kt) | [test](src/test/kotlin/exercise/easy/id977/Solution977Test.kt) | Easy |
58+
| 57 | [Insert Internal](src/main/kotlin/exercise/medium/id57/Description57.md) | [solution](src/main/kotlin/exercise/medium/id57/Solution57.kt) | [test](src/test/kotlin/exercise/medium/id57/Solution57Test.kt) | Medium |
5859

5960
[//]: # (https://www.techinterviewhandbook.org/grind75?weeks=26&hours=40&grouping=topics)
6061

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
[//]: # (Copyright [2023] [Anton Kotler kotler.developer@gmail.com] License MIT)
2+
3+
# Insert Internal
4+
5+
![Difficulty](https://img.shields.io/badge/Difficulty-Medium-548af7)
6+
7+
You are given an array of non-overlapping intervals `intervals` where `intervals[i] = [start(i), end(i)]` represent the
8+
start and the end of the `i'th` interval and `intervals` is sorted in ascending order by `start(i)`. You are also given
9+
an interval `newInterval = [start, end]` that represents the start and end of another interval.
10+
11+
Insert `newInrerval` into `intervals` such that `intervals` is still sorted in ascending order by `start(i)`
12+
and `intervals` still does not have any overlaping intervals (merged overlaping intervals if necessary).
13+
14+
Return `intervals` after the insertion.
15+
16+
Example 1:
17+
18+
```
19+
Input: intervals = [[1,3], [6,9]], newIntervals = [2,5]
20+
Output: [[1,5],[6,9]]
21+
```
22+
23+
Example 2:
24+
25+
```
26+
Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], target = 6
27+
Output: [[1,2],[3,10],[12,16]]
28+
Explanation: Because the new interval [4,8] overlaping with [3,5],[6,7],[8,10]
29+
```
30+
31+
Example 3:
32+
33+
```
34+
Input: intervals = [3,3], target = 6
35+
Output: [0,1]
36+
```
37+
38+
Constraints:
39+
40+
- `0 <= intervals.length <= 104`
41+
- `intervals[i].length == 2`
42+
- `0 <= starti <= endi <= 105`
43+
- `intervals` is sorted by `start(i)` in ascending order
44+
- newInterval.length == 2
45+
- `0 <= start <= end <= 105`
46+
47+
| ID | Description | Solution | Test | Difficulty |
48+
|:--:|:----------------|:---------------------------:|:--------------------------------------------------------------------------------:|:----------:|
49+
| 57 | Insert Interval | [solution](./Solution57.kt) | [test](../../../../../../src/test/kotlin/exercise/medium/id57/Solution57Test.kt) | Medium |
50+
51+
:top: [Back to all topics](https://github.com/kotler-dev/kotlin-leetcode)
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
package exercise.medium.id57
2+
3+
import kotlin.math.max
4+
import kotlin.math.min
5+
6+
class Solution57 {
7+
fun insert(intervals: Array<IntArray>, newInterval: IntArray): Array<IntArray> {
8+
9+
if (intervals.isEmpty()) return arrayOf(newInterval)
10+
if (newInterval.isEmpty()) return intervals
11+
12+
println("[${newInterval[0]}, ${newInterval[1]}]")
13+
14+
var start = newInterval[0]
15+
var end = newInterval[1]
16+
val left = 0
17+
val right = 1
18+
19+
val merged = mutableListOf<IntArray>()
20+
var index = 0
21+
while (index < intervals.size && intervals[index][right] < start) {
22+
merged.add(intervals[index])
23+
index++
24+
}
25+
26+
27+
while (index < intervals.size && intervals[index][left] <= end) {
28+
start = min(start, intervals[index][left])
29+
end = max(end, intervals[index][right])
30+
index++
31+
}
32+
33+
merged.add(intArrayOf(start, end))
34+
35+
while (index < intervals.size) {
36+
merged.add(intervals[index])
37+
index++
38+
}
39+
40+
return merged.toTypedArray()
41+
}
42+
/*
43+
44+
fun insert(intervals: Array<IntArray>, newInterval: IntArray): Array<IntArray> {
45+
46+
if (intervals.isEmpty()) return arrayOf(newInterval)
47+
48+
println("[${newInterval[0]}, ${newInterval[1]}]")
49+
50+
val start = newInterval[0]
51+
val end = newInterval[1]
52+
val left = 0
53+
val right = 1
54+
55+
val merged1 = mutableListOf<IntArray>()
56+
var index1 = 0
57+
while (index1 < intervals.size && intervals[index1][right] < start) {
58+
merged1.add(intervals[index1])
59+
index1++
60+
}
61+
62+
if (intervals.size == 1) index1--
63+
64+
val merged2 = mutableListOf<IntArray>()
65+
var index2 = intervals.size - 1
66+
while (index2 >= 0 && intervals[index2][left] > end) {
67+
merged2.add(intervals[index2])
68+
index2--
69+
}
70+
71+
if (intervals.size > 1) {
72+
val min = min(start, intervals[index1][left])
73+
val max = max(end, intervals[index2][right])
74+
merged1.add(intArrayOf(min, max))
75+
}
76+
merged2.forEach { merged1.add(it) }
77+
78+
return merged1.toTypedArray()
79+
}
80+
81+
var index2 = index
82+
while (index2 < intervals.size) {
83+
if (intervals[index2][left] > end) {
84+
array.add(intervals[index2])
85+
index2++
86+
}
87+
}
88+
89+
for ((index, arr) in intervals.withIndex()) {
90+
if (arr[right] < start) {
91+
array.add(arr)
92+
}
93+
}
94+
95+
96+
while (index < intervals.size ) {
97+
val value = intervals[index][right]
98+
if (value < start) {
99+
array.add(intervals[index])
100+
} else if (value < intervals[index][leftIndex]) {
101+
intervals[index][right] = end
102+
array.add(intervals[index])
103+
sliceStart = index
104+
break
105+
}
106+
}
107+
108+
for (index in intervals.indices) {
109+
val value = intervals[index][rightIndex]
110+
if (value < start) {
111+
array.add(intervals[index])
112+
} else if (value < intervals[index][leftIndex]) {
113+
intervals[index][rightIndex] = end
114+
array.add(intervals[index])
115+
sliceStart = index
116+
break
117+
}
118+
}
119+
120+
fun insert(intervals: Array<IntArray>, newInterval: IntArray): Array<IntArray> {
121+
122+
println("[${newInterval[0]}, ${newInterval[1]}]")
123+
124+
val arrayFlatten = mutableListOf<Int>()
125+
for (index in intervals.indices) {
126+
arrayFlatten.add(intervals[index][0])
127+
arrayFlatten.add(intervals[index][1])
128+
}
129+
130+
val arrayNonOverlapping = mutableListOf<Int>()
131+
for (index in 0..<arrayFlatten.size) {
132+
if (arrayFlatten[index] !in newInterval[0]..newInterval[1]) {
133+
println("==> ${arrayFlatten[index]}")
134+
arrayNonOverlapping.add(arrayFlatten[index])
135+
136+
}
137+
}
138+
val arr = mutableListOf<IntArray>()
139+
for (index in 0..arrayNonOverlapping.size - 1 step 2) {
140+
arr.add(intArrayOf(arrayNonOverlapping[index], arrayNonOverlapping[index + 1]))
141+
}
142+
143+
return arr.toTypedArray()
144+
}
145+
146+
fun insert(intervals: Array<IntArray>, newInterval: IntArray): Array<IntArray> {
147+
println("[${newInterval[0]}, ${newInterval[1]}]")
148+
val list = intervals.flatMap { it.asIterable() }.toMutableList()
149+
val start = newInterval[0]
150+
val end = newInterval[1]
151+
if (start !in list) list.add(start)
152+
if (end !in list) list.add(end)
153+
list.sort()
154+
var shiftLeft = 1
155+
var shiftRight = 1
156+
if (list.size % 2 != 0) shiftLeft--
157+
val part1: List<Int> = list.slice(0..<list.indexOf(start) - shiftLeft)
158+
if (part1.size % 2 == 0) shiftRight++
159+
val part2: List<Int> = list.slice(list.indexOf(end) + shiftRight..<list.size)
160+
val part3: List<Int> = listOf(part1, part2).flatten()
161+
val arr2 = mutableListOf<IntArray>()
162+
for (index in 0..part3.size - 1 step 2) {
163+
arr2.add(intArrayOf(part3[index], part3[index + 1]))
164+
}
165+
return arr2.toTypedArray()
166+
}
167+
168+
*/
169+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package exercise.medium.id57
2+
3+
import io.kotest.assertions.print.print
4+
import io.kotest.core.spec.style.FunSpec
5+
import io.kotest.datatest.WithDataTestName
6+
import io.kotest.datatest.withData
7+
import io.kotest.matchers.shouldBe
8+
9+
data class TestCase(
10+
val intervals: Array<IntArray>,
11+
val newInterval: IntArray,
12+
val expected: Array<IntArray>,
13+
) : WithDataTestName {
14+
override fun dataTestName(): String = expected.print().value
15+
}
16+
17+
class Solution57Test : FunSpec({
18+
context("shouldBe") {
19+
withData(
20+
nameFn = { "Input: ${it.intervals.print().value}, newInterval: ${it.newInterval.print().value}, Expected: ${it.expected.print().value}" },
21+
TestCase(
22+
intervals = arrayOf(
23+
intArrayOf(1, 2),
24+
intArrayOf(3, 5),
25+
intArrayOf(6, 7),
26+
intArrayOf(8, 10),
27+
intArrayOf(12, 16)
28+
),
29+
newInterval = intArrayOf(4, 8),
30+
expected = arrayOf(intArrayOf(1, 2), intArrayOf(3, 10), intArrayOf(12, 16))
31+
),
32+
TestCase(
33+
intervals = arrayOf(intArrayOf(1, 3), intArrayOf(6, 9)),
34+
newInterval = intArrayOf(2, 5),
35+
expected = arrayOf(intArrayOf(1, 5), intArrayOf(6, 9))
36+
),
37+
TestCase(
38+
intervals = arrayOf(),
39+
newInterval = intArrayOf(2, 5),
40+
expected = arrayOf(intArrayOf(2, 5))
41+
),
42+
TestCase(
43+
intervals = arrayOf(intArrayOf(1, 5)),
44+
newInterval = intArrayOf(6, 8),
45+
expected = arrayOf(intArrayOf(1, 5), intArrayOf(6, 8))
46+
),
47+
TestCase(
48+
intervals = arrayOf(intArrayOf(1, 5)),
49+
newInterval = intArrayOf(2, 3),
50+
expected = arrayOf(intArrayOf(1, 5))
51+
)
52+
) { (intervals, newInterval, expected) ->
53+
Solution57().insert(intervals, newInterval) shouldBe expected
54+
}
55+
}
56+
})

0 commit comments

Comments
 (0)