File tree Expand file tree Collapse file tree 2 files changed +50
-2
lines changed
contest/src/main/java/com/github/contest Expand file tree Collapse file tree 2 files changed +50
-2
lines changed Original file line number Diff line number Diff line change 11package com.github.contest
22
33
4- import com.github.contest.design.wrapper
4+ import com.github.contest.design.RLEIterator
55import com.github.contest.math.numberOfPowerfulInt
66import com.github.contest.slidingWindow.customStructure.rabinKarpMultiPattern
77import com.github.contest.slidingWindow.customStructure.slidingWindowClassic
@@ -16,7 +16,14 @@ import java.util.TreeMap
1616
1717fun 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
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments