-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path900.java
More file actions
63 lines (57 loc) · 1.55 KB
/
900.java
File metadata and controls
63 lines (57 loc) · 1.55 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
__________________________________________________________________________________________________
sample 44 ms submission
class RLEIterator {
int[] a;
int idx;
public RLEIterator(int[] A) {
a = A;
idx = 0;
}
public int next(int n) {
while(n > 0 && idx < a.length) {
if (n > a[idx]) {
n -= a[idx];
if (n == 0) return a[idx + 1];
idx += 2;
} else {
a[idx] -= n;
return a[idx + 1];
}
}
return -1;
}
}
/**
* Your RLEIterator object will be instantiated and called as such:
* RLEIterator obj = new RLEIterator(A);
* int param_1 = obj.next(n);
*/
__________________________________________________________________________________________________
sample 38468 kb submission
class RLEIterator {
private int currIndex;
private int[] A;
public RLEIterator(int[] A) {
currIndex = 0;
this.A = A;
}
public int next(int n) {
while(currIndex < A.length) {
if(A[currIndex] < n) {
n -= A[currIndex];
currIndex += 2;
}
else {
A[currIndex] -= n;
return A[currIndex+1];
}
}
return -1;
}
}
/**
* Your RLEIterator object will be instantiated and called as such:
* RLEIterator obj = new RLEIterator(A);
* int param_1 = obj.next(n);
*/
__________________________________________________________________________________________________