<jupyter_start><jupyter_text>说明：    编写一个遍历游程编码序列的迭代器。    迭代器由RLEIterator（int [] A）初始化，其中A是某个序列的行程编码。    更具体地说，对于所有偶数i，A [i]告诉我们在序列中重复非负整数值A [i+1]的次数。    迭代器支持一个函数：next（int n），它耗尽接下来的n个元素（n> = 1），并返回以这种方式耗尽的最后一个元素。    如果没有元素要用尽，则next返回-1。例如，我们从A = [3,8,0,9,2,5]开始，它是序列[8,8,8,5,5]的行程编码。    这是因为该序列可以读为“三八，零九，二五”。Example 1:    Input: ["RLEIterator","next","next","next","next"],            [[[3,8,0,9,2,5]],[2],[1],[1],[2]]    Output:             [null,8,8,5,-1]    Explanation:         RLEIterator is initialized with RLEIterator([3,8,0,9,2,5]).        This maps to the sequence [8,8,8,5,5].        RLEIterator.next is then called 4 times:    .next(2) exhausts 2 terms of the sequence, returning 8.  The remaining sequence is now [8, 5, 5].    .next(1) exhausts 1 term of the sequence, returning 8.  The remaining sequence is now [5, 5].        .next(1) exhausts 1 term of the sequence, returning 5.  The remaining sequence is now [5].    .next(2) exhausts 2 terms, returning -1.  This is because the first term exhausted was 5,    but the second term did not exist.  Since the last term exhausted does not exist, we return -1.Note:    1、0 <= A.length <= 1000    2、A.length is an even integer.    3、0 <= A[i] <= 10^9    4、There are at most 1000 calls to RLEIterator.next(int n) per test case.    5、Each call to RLEIterator.next(int n) will have 1 <= n <= 10^9.<jupyter_code>class RLEIterator:
    def __init__(self, A):
        self.A = A[::-1]
    
    def next(self, n: int) -> int:
        acc = 0
        while acc < n:
            if not self.A:
                return -1
            times, num = self.A.pop(), self.A.pop()
            acc += times
        if acc > n:
            self.A.append(num)
            self.A.append(acc - n)
        print(self.A)
        return num
solution = RLEIterator([3,8,0,9,2,5])
param_1 = solution.next(2)
param_2 = solution.next(1)
param_3 = solution.next(1)
param_4 = solution.next(2)
print(param_1, param_2, param_3, param_4)
class RLEIterator:
    def __init__(self, A):
        self.a_freq = []
        for i, a in enumerate(A):
            if i % 2 == 0 and a != 0:
                self.a_freq.append([A[i+1], a])
        self.idx = 0

    def next(self, n: int) -> int:
        if not self.a_freq:
            return -1
        for i in range(len(self.a_freq)):
            item = self.a_freq[i]
            cur_time = item[1]
            if cur_time >= n:
                self.a_freq[i][1] -= n
                return self.a_freq[i][0]
        self.a_freq.pop(0)
        self.next(n - cur_time)
solution = RLEIterator([3,8,0,9,2,5])
param_1 = solution.next(2)
param_2 = solution.next(1)
param_3 = solution.next(1)
param_4 = solution.next(2)
print(param_1, param_2, param_3, param_4)
a = [1, 2]
a.extend([1]*3)
a
def test():
    return 1

def test_2():
    yield test()
int(test_2())
class RLEIterator:
    def __init__(self, A):
        self.a_freq = []
        for i, a in enumerate(A):
            if i % 2 == 0 and a != 0:
                self.a_freq.extend([A[i+1]] * a)
        self.a_freq = self.a_freq[::-1]

    def next(self, n: int) -> int:
        if len(self.a_freq) < n:
            return -1
        for i in range(n-1):
            self.a_freq.pop()
        return self.a_freq.pop()
class RLEIterator:
    def __init__(self, A):
        self.a = A[::-1]
        self.res = None
        
    def next(self, n: int) -> int:
        if not self.a:
            self.res = -1
        elif n <= self.a[-1]:
            self.a[-1] -= n
            self.res = self.a[-2]
        else:
            n -= self.a[-1]
            self.a.pop()
            self.a.pop()
            self.next(n)
        return self.res
solution = RLEIterator([3,8,0,9,2,5])
param_1 = solution.next(2)
param_2 = solution.next(1)
param_3 = solution.next(1)
param_4 = solution.next(2)
print(param_1, param_2, param_3, param_4)
a = [1, 2]
print(a[-2])
class RLEIterator:

    def __init__(self, A):
        
        self.q = A
        self.i = 0
        
    def next(self, n: int) -> int:
        
        while n > 0:
            while self.i < len(self.q):
                if self.q[self.i] < n:
                    n -= self.q[self.i]
                    self.i += 2
                else:
                    self.q[self.i] -= n
                    return self.q[self.i+1]
            return -1<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14541695382494832, 'fraction_numerical': 0.04089133930622559, 'mean_word_length': 1.9280430396772024, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '515807', 'license': 'Apache-2.0', 'n_tokens_mistral': 1736, 'n_tokens_neox': 1698, 'n_words': 404, 'path': 'Array/0923/900. RLE Iterator.ipynb', 'repo_name': 'YuHe0108/Leetcode', 'size': '4353'}