Skip to content

Commit 0a15ad9

Browse files
Merge pull request #204
add new problem 2.06
2 parents 42499cf + 33b4dd4 commit 0a15ad9

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

contest/src/main/java/com/github/contest/Execute.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.github.contest
22

33

4-
import com.github.contest.design.wrapper
4+
import com.github.contest.design.RLEIterator
55
import com.github.contest.math.numberOfPowerfulInt
66
import com.github.contest.slidingWindow.customStructure.rabinKarpMultiPattern
77
import com.github.contest.slidingWindow.customStructure.slidingWindowClassic
@@ -16,7 +16,14 @@ import java.util.TreeMap
1616

1717
fun main() {
1818

19-
wrapper()
19+
val rleIterator = RLEIterator(intArrayOf(5, 2, 1, 22))
20+
rleIterator.next(5).also { println(it) }
21+
rleIterator.next(2).also { println(it) }
22+
rleIterator.next(1).also { println(it) }
23+
}
24+
25+
infix fun Int.myRange(to: Int): IntRange {
26+
return this..to
2027
}
2128

2229

contest/src/main/java/com/github/contest/design/DesignLeetcode.kt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,45 @@ class NestedIterator(nestedList: List<NestedInteger>) {
290290
fun next(): Int = store.removeFirst()
291291

292292
fun hasNext(): Boolean = store.isNotEmpty()
293+
}
294+
295+
/**
296+
* 900. RLE Iterator
297+
*/
298+
299+
class RLEIterator(private val encoding: IntArray) {
300+
301+
private var count = 0L
302+
303+
init {
304+
for (i in encoding.indices step 2) count += encoding[i].toLong()
305+
}
306+
307+
fun next(n: Int): Int {
308+
if (n > count) {
309+
count = -1
310+
return -1
311+
}
312+
313+
var n = n
314+
for (i in encoding.indices step 2) {
315+
316+
when {
317+
n <= encoding[i] -> {
318+
encoding[i] -= n
319+
count -= n
320+
return encoding[i + 1]
321+
}
322+
323+
else -> {
324+
n -= encoding[i]
325+
count -= encoding[i]
326+
encoding[i] = 0
327+
}
328+
}
329+
}
330+
331+
return 0
332+
}
333+
293334
}

0 commit comments

Comments
 (0)