900 RLE Iterator RLE 迭代器
Description:
We can use run-length encoding (i.e., RLE) to encode a sequence of integers. In a run-length encoded array of even length encoding (0-indexed), for all even i, encoding[i] tells us the number of times that the non-negative integer value encoding[i + 1] is repeated in the sequence.
For example, the sequence arr = [8,8,8,5,5] can be encoded to be encoding = [3,8,2,5]. encoding = [3,8,0,9,2,5] and encoding = [2,8,1,8,2,5] are also valid RLE of arr.
Given a run-length encoded array, design an iterator that iterates through it.
Implement the RLEIterator class:
RLEIterator(int[] encoded) Initializes the object with the encoded array encoded.
int next(int n) Exhausts the next n elements and returns the last element exhausted in this way. If there is no element left to exhaust, return -1 instead.
Example:
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 rLEIterator = new RLEIterator([3, 8, 0, 9, 2, 5]); // This maps to the sequence [8,8,8,5,5].
rLEIterator.next(2); // exhausts 2 terms of the sequence, returning 8. The remaining sequence is now [8, 5, 5].
rLEIterator.next(1); // exhausts 1 term of the sequence, returning 8. The remaining sequence is now [5, 5].
rLEIterator.next(1); // exhausts 1 term of the sequence, returning 5. The remaining sequence is now [5].
rLEIterator.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.
Constraints:
2 <= encoding.length <= 1000
encoding.length is even.
0 <= encoding[i] <= 10^9
1 <= n <= 10^9
At most 1000 calls will be made to next.
題目描述:
編寫一個遍歷遊程編碼序列的迭代器。
迭代器由 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] 的遊程編碼。這是因爲該序列可以讀作 “三個八,零個九,兩個五”。
示例 :
輸入:["RLEIterator","next","next","next","next"], [[[3,8,0,9,2,5]],[2],[1],[1],[2]]
輸出:[null,8,8,5,-1]
解釋:
RLEIterator 由 RLEIterator([3,8,0,9,2,5]) 初始化。
這映射到序列 [8,8,8,5,5]。
然後調用 RLEIterator.next 4次。
RLEIterator.next(2) 耗去序列的 2 個項,返回 8。現在剩下的序列是 [8, 5, 5]。
RLEIterator.next(1) 耗去序列的 1 個項,返回 8。現在剩下的序列是 [5, 5]。
RLEIterator.next(1) 耗去序列的 1 個項,返回 5。現在剩下的序列是 [5]。
RLEIterator.next(2) 耗去序列的 2 個項,返回 -1。 這是由於第一個被耗去的項是 5,
但第二個項並不存在。由於最後一個要耗去的項不存在,我們返回 -1。
提示:
0 <= A.length <= 1000
A.length 是偶數。
0 <= A[i] <= 10^9
每個測試用例最多調用 1000 次 RLEIterator.next(int n)。
每次調用 RLEIterator.next(int n) 都有 1 <= n <= 10^9 。
思路:
模擬
記錄下標的位置
如果下標指向的元素個數小於 n, 則移動下標, 並更新 n
否則減少下標指向的元素個數, 並將 n 置零
返回時比較下標和 encoding 的長度返回 -1 或對應位置的元素
時間複雜度爲 O(n), 空間複雜度爲 O(n)
代碼:
C++:
class RLEIterator
{
private:
vector<int> nums;
int index, length;
public:
RLEIterator(vector<int>& encoding)
{
nums = encoding;
index = 0;
length = encoding.size();
}
int next(int n)
{
while (index < length and n)
{
if (nums[index] >= n)
{
nums[index] -= n;
n = 0;
}
else
{
n -= nums[index];
index += 2;
}
}
return index < length ? nums[index + 1] : -1;
}
};
/**
* Your RLEIterator object will be instantiated and called as such:
* RLEIterator* obj = new RLEIterator(encoding);
* int param_1 = obj->next(n);
*/
Java:
class RLEIterator {
private int[] nums;
private int index, size;
public RLEIterator(int[] encoding) {
index = 0;
nums = encoding;
size = encoding.length;
}
public int next(int n) {
while (index < size && n > 0) {
if (nums[index] >= n) {
nums[index] -= n;
n = 0;
} else {
n -= nums[index];
index += 2;
}
}
return index >= size ? -1 : nums[index + 1];
}
}
/**
* Your RLEIterator object will be instantiated and called as such:
* RLEIterator obj = new RLEIterator(encoding);
* int param_1 = obj.next(n);
*/
Python:
class RLEIterator:
def __init__(self, encoding: List[int]):
self.encoding = encoding[::-1]
self.index = 0
def next(self, n: int) -> int:
while self.encoding and n > 0:
if n > self.encoding[-1]:
n -= self.encoding.pop()
self.encoding.pop()
else:
self.encoding[-1] -= n
n = 0
return -1 if not self.encoding else self.encoding[-2]
# Your RLEIterator object will be instantiated and called as such:
# obj = RLEIterator(encoding)
# param_1 = obj.next(n)