-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy path900.cpp
78 lines (74 loc) · 1.87 KB
/
900.cpp
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
__________________________________________________________________________________________________
sample 8 ms submission
class RLEIterator {
public:
vector<int> A;
vector<int> B;
int64_t i = 0;
int64_t ii = 0;
RLEIterator(vector<int>& a) {
for(int i = 0; i < a.size(); i+=2) {
A.push_back(a[i]);
B.push_back(a[i+1]);
}
}
int next(int n) {
while(true) {
if(A[i] == 0) {
i++;
continue;
}
if(i >= A.size()) {
return -1;
}
if(ii + n <= A[i]) {
ii += n;
int ret = B[i];
if(ii == A[i]) {
ii = 0;
i++;
}
return ret;
}
n -= (A[i] - ii);
ii = 0;
i++;
}
}
};
/**
* Your RLEIterator object will be instantiated and called as such:
* RLEIterator* obj = new RLEIterator(A);
* int param_1 = obj->next(n);
*/
__________________________________________________________________________________________________
sample 10024 kb submission
class RLEIterator {
vector<int> arr;
int idx, used;
public:
RLEIterator(vector<int> arr) {
this->arr=arr;
idx=used=0;
}
int next(int n) {
while (idx<arr.size()){
if (arr[idx]-used<n){
n-=(arr[idx]-used);
idx+=2;
used=0;
}
else{
used+=n;
return arr[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);
*/
__________________________________________________________________________________________________