-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0900-rle-iterator.py
More file actions
26 lines (21 loc) · 651 Bytes
/
0900-rle-iterator.py
File metadata and controls
26 lines (21 loc) · 651 Bytes
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
# time complexity: O(n)
# space complexity: O(n)
from typing import List
class RLEIterator:
def __init__(self, encoding: List[int]):
self.encoding = encoding[::-1]
def next(self, n: int) -> int:
acc = 0
while acc < n:
if not self.encoding:
return -1
times, num = self.encoding.pop(), self.encoding.pop()
acc += times
self.encoding.append(num)
self.encoding.append(acc - n)
return num
rLEIterator = RLEIterator([3, 8, 0, 9, 2, 5])
print(rLEIterator.next(2))
print(rLEIterator.next(1))
print(rLEIterator.next(1))
print(rLEIterator.next(2))