forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_900.java
37 lines (32 loc) · 1.01 KB
/
_900.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.fishercoder.solutions;
public class _900 {
public static class Solution1 {
public static class RLEIterator {
int index;
int[] array;
public RLEIterator(int[] A) {
index = 0;
array = A;
}
public int next(int n) {
int lastElement = -1;
while (n > 0 && index < array.length) {
if (array[index] > n) {
array[index] -= n;
lastElement = array[index + 1];
break;
} else if (array[index] == n) {
array[index] = 0;
lastElement = array[index + 1];
index += 2;
break;
} else {
n -= array[index];
index += 2;
}
}
return lastElement;
}
}
}
}