From 2708d92199e07a3b4f94129e76fcba3532466599 Mon Sep 17 00:00:00 2001 From: liuyubobobo Date: Mon, 10 Sep 2018 16:55:05 -0700 Subject: [PATCH] 0900 solved. --- 0900-RLE-Iterator/cpp-0900/CMakeLists.txt | 7 +++ 0900-RLE-Iterator/cpp-0900/main.cpp | 55 +++++++++++++++++++++++ readme.md | 1 + 3 files changed, 63 insertions(+) create mode 100644 0900-RLE-Iterator/cpp-0900/CMakeLists.txt create mode 100644 0900-RLE-Iterator/cpp-0900/main.cpp diff --git a/0900-RLE-Iterator/cpp-0900/CMakeLists.txt b/0900-RLE-Iterator/cpp-0900/CMakeLists.txt new file mode 100644 index 00000000..4361a6de --- /dev/null +++ b/0900-RLE-Iterator/cpp-0900/CMakeLists.txt @@ -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}) \ No newline at end of file diff --git a/0900-RLE-Iterator/cpp-0900/main.cpp b/0900-RLE-Iterator/cpp-0900/main.cpp new file mode 100644 index 00000000..6ad2d940 --- /dev/null +++ b/0900-RLE-Iterator/cpp-0900/main.cpp @@ -0,0 +1,55 @@ +/// Source : https://leetcode.com/problems/rle-iterator/description/ +/// Author : liuyubobobo +/// Time : 2018-09-10 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: init: O(n) +/// next: O(n) +/// Space Complexity: O(n) +class RLEIterator { + +private: + vector A; + int curnum_index = 1, index = 0; + +public: + RLEIterator(vector 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 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; +} \ No newline at end of file diff --git a/readme.md b/readme.md index d2c9dbc4..fa72d4b8 100644 --- a/readme.md +++ b/readme.md @@ -503,4 +503,5 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 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/) | | | | | | | | | | \ No newline at end of file