Skip to content

Commit

Permalink
0900 solved.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyubobobo committed Sep 10, 2018
1 parent 04c1009 commit 2708d92
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
7 changes: 7 additions & 0 deletions 0900-RLE-Iterator/cpp-0900/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.5)
project(cpp_0900)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp)
add_executable(cpp_0900 ${SOURCE_FILES})
55 changes: 55 additions & 0 deletions 0900-RLE-Iterator/cpp-0900/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -503,4 +503,5 @@ email: [[email protected]](mailto:[email protected])
| 897 | [Increasing Order Search Tree](https://leetcode.com/problems/increasing-order-search-tree/description/) | [solution](https://leetcode.com/problems/increasing-order-search-tree/solution/) | [C++](0897-Increasing-Order-Search-Tree/cpp-0897/) | | |
| 898 | [Bitwise ORs of Subarrays](https://leetcode.com/problems/bitwise-ors-of-subarrays/description/) | [solution](https://leetcode.com/problems/bitwise-ors-of-subarrays/solution/) | [C++](0898-Bitwise-ORs-of-Subarrays/cpp-0898/) | | |
| 899 | [Orderly Queue](https://leetcode.com/problems/orderly-queue/description/) | [solution](https://leetcode.com/problems/orderly-queue/solution/) | [C++](0899-Orderly-Queue/cpp-0899/) | | |
| 900 | [RLE Iterator](https://leetcode.com/problems/rle-iterator/description/) | [solution](https://leetcode.com/problems/rle-iterator/solution/) | [C++](0900-RLE-Iterator/cpp-0900/) | | |
| | | | | | |

0 comments on commit 2708d92

Please sign in to comment.