|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +/** |
| 4 | + * 900. RLE Iterator |
| 5 | + * |
| 6 | + * Write an iterator that iterates through a run-length encoded sequence. |
| 7 | + * |
| 8 | + * The iterator is initialized by RLEIterator(int[] A), where A is a run-length encoding of some sequence. |
| 9 | + * More specifically, for all even i, A[i] tells us the number of times that the non-negative integer value A[i+1] is repeated in the sequence. |
| 10 | + * |
| 11 | + * The iterator supports one function: next(int n), |
| 12 | + * which exhausts the next n elements (n >= 1) and returns the last element exhausted in this way. |
| 13 | + * If there is no element left to exhaust, next returns -1 instead. |
| 14 | + * |
| 15 | + * For example, we start with A = [3,8,0,9,2,5], which is a run-length encoding of the |
| 16 | + * sequence [8,8,8,5,5]. |
| 17 | + * This is because the sequence can be read as "three eights, zero nines, two fives". |
| 18 | + * |
| 19 | + * Example 1: |
| 20 | + * |
| 21 | + * Input: ["RLEIterator","next","next","next","next"], [[[3,8,0,9,2,5]],[2],[1],[1],[2]] |
| 22 | + * Output: [null,8,8,5,-1] |
| 23 | + * Explanation: |
| 24 | + * RLEIterator is initialized with RLEIterator([3,8,0,9,2,5]). |
| 25 | + * This maps to the sequence [8,8,8,5,5]. |
| 26 | + * RLEIterator.next is then called 4 times: |
| 27 | + * .next(2) exhausts 2 terms of the sequence, returning 8. The remaining sequence is now [8, 5, 5]. |
| 28 | + * .next(1) exhausts 1 term of the sequence, returning 8. The remaining sequence is now [5, 5]. |
| 29 | + * .next(1) exhausts 1 term of the sequence, returning 5. The remaining sequence is now [5]. |
| 30 | + * .next(2) exhausts 2 terms, returning -1. This is because the first term exhausted was 5, |
| 31 | + * but the second term did not exist. Since the last term exhausted does not exist, we return -1. |
| 32 | + * |
| 33 | + * Note: |
| 34 | + * |
| 35 | + * 0 <= A.length <= 1000 |
| 36 | + * A.length is an even integer. |
| 37 | + * 0 <= A[i] <= 10^9 |
| 38 | + * There are at most 1000 calls to RLEIterator.next(int n) per test case. |
| 39 | + * Each call to RLEIterator.next(int n) will have 1 <= n <= 10^9. |
| 40 | + */ |
| 41 | +public class _900 { |
| 42 | + public static class Solution1 { |
| 43 | + public static class RLEIterator { |
| 44 | + |
| 45 | + int index; |
| 46 | + int[] array; |
| 47 | + |
| 48 | + public RLEIterator(int[] A) { |
| 49 | + index = 0; |
| 50 | + array = A; |
| 51 | + } |
| 52 | + |
| 53 | + public int next(int n) { |
| 54 | + int lastElement = -1; |
| 55 | + while (n > 0 && index < array.length) { |
| 56 | + if (array[index] > n) { |
| 57 | + array[index] -= n; |
| 58 | + lastElement = array[index + 1]; |
| 59 | + break; |
| 60 | + } else if (array[index] == n) { |
| 61 | + array[index] = 0; |
| 62 | + lastElement = array[index + 1]; |
| 63 | + index += 2; |
| 64 | + break; |
| 65 | + } else { |
| 66 | + n -= array[index]; |
| 67 | + index += 2; |
| 68 | + } |
| 69 | + } |
| 70 | + return lastElement; |
| 71 | + } |
| 72 | + |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments