forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
55 lines (41 loc) · 1.13 KB
/
main.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
/// Source : https://leetcode.com/problems/rle-iterator/description/
/// Author : liuyubobobo
/// Time : 2018-09-10
#include <iostream>
#include <vector>
using namespace std;
/// Ad-Hoc
/// Time Complexity: init: O(n)
/// next: O(n)
/// Space Complexity: O(n)
class RLEIterator {
private:
vector<int> A;
int curnum_index = 1, index = 0;
public:
RLEIterator(vector<int> A) {
for(int a: A)
this->A.push_back(a);
}
int next(int n) {
if(curnum_index >= A.size())
return -1;
index += n;
while(curnum_index < A.size() && index > A[curnum_index - 1]){
index -= A[curnum_index - 1];
curnum_index += 2;
}
if(curnum_index < A.size() && index <= A[curnum_index - 1])
return A[curnum_index];
return -1;
}
};
int main() {
vector<int> A = {3, 8, 0, 9, 2, 5};
RLEIterator rleIterator(A);
cout << rleIterator.next(2) << endl; // 8
cout << rleIterator.next(1) << endl; // 8
cout << rleIterator.next(1) << endl; // 5
cout << rleIterator.next(2) << endl; // -1
return 0;
}