Skip to content

Commit 2282f6a

Browse files
refactor 900
1 parent fcf1206 commit 2282f6a

File tree

1 file changed

+28
-66
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+28
-66
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,37 @@
11
package com.fishercoder.solutions;
22

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-
*/
413
public class _900 {
42-
public static class Solution1 {
43-
public static class RLEIterator {
4+
public static class Solution1 {
5+
public static class RLEIterator {
446

45-
int index;
46-
int[] array;
7+
int index;
8+
int[] array;
479

48-
public RLEIterator(int[] A) {
49-
index = 0;
50-
array = A;
51-
}
10+
public RLEIterator(int[] A) {
11+
index = 0;
12+
array = A;
13+
}
5214

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-
}
15+
public int next(int n) {
16+
int lastElement = -1;
17+
while (n > 0 && index < array.length) {
18+
if (array[index] > n) {
19+
array[index] -= n;
20+
lastElement = array[index + 1];
21+
break;
22+
} else if (array[index] == n) {
23+
array[index] = 0;
24+
lastElement = array[index + 1];
25+
index += 2;
26+
break;
27+
} else {
28+
n -= array[index];
29+
index += 2;
30+
}
31+
}
32+
return lastElement;
33+
}
7234

35+
}
7336
}
74-
}
7537
}

0 commit comments

Comments
 (0)