Skip to content

Commit d71ebdf

Browse files
add 900
1 parent 093d87b commit d71ebdf

File tree

3 files changed

+111
-1
lines changed

3 files changed

+111
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Your ideas/fixes/algorithms are more than welcome!
5252
|922|[Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_922.java) | O(n) | O(1) | |Easy|
5353
|917|[Reverse Only Letters](https://leetcode.com/problems/reverse-only-letters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_917.java) | O(n) | O(n) | |Easy|
5454
|908|[Smallest Range I](https://leetcode.com/problems/smallest-range-i/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_908.java) | O(nlogn) | O(1) | |Easy|
55-
|900|[Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_900.java) | O(n) | O(1) | |Easy|
55+
|900|[RLE Iterator](https://leetcode.com/problems/rle-iterator/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_900.java) | O(n) | O(1) | |Medium|
5656
|897|[Increasing Order Search Tree](https://leetcode.com/problems/increasing-order-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_897.java) | O(n) | O(n) | |Easy| DFS, recursion
5757
|896|[Monotonic Array](https://leetcode.com/problems/monotonic-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_896.java) | O(n) | O(1) | |Easy|
5858
|884|[Uncommon Words from Two Sentences](https://leetcode.com/problems/uncommon-words-from-two-sentences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_884.java) | O(n) | O(k) | |Easy|
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._900;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertEquals;
7+
8+
public class _900Test {
9+
private static _900.Solution1.RLEIterator rleIterator;
10+
11+
@Test
12+
public void test1() {
13+
rleIterator = new _900.Solution1.RLEIterator(new int[] {3, 8, 0, 9, 2, 5});
14+
assertEquals(8, rleIterator.next(2));
15+
assertEquals(8, rleIterator.next(1));
16+
assertEquals(5, rleIterator.next(1));
17+
assertEquals(-1, rleIterator.next(2));
18+
}
19+
20+
@Test
21+
public void test2() {
22+
rleIterator = new _900.Solution1.RLEIterator(
23+
new int[] {811, 903, 310, 730, 899, 684, 472, 100, 434, 611});
24+
assertEquals(903, rleIterator.next(358));
25+
assertEquals(903, rleIterator.next(345));
26+
assertEquals(730, rleIterator.next(154));
27+
assertEquals(684, rleIterator.next(265));
28+
assertEquals(684, rleIterator.next(73));
29+
assertEquals(684, rleIterator.next(220));
30+
assertEquals(684, rleIterator.next(138));
31+
assertEquals(684, rleIterator.next(4));
32+
assertEquals(684, rleIterator.next(170));
33+
assertEquals(684, rleIterator.next(88));
34+
}
35+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy