From 0c118cb0feb578f044baf6b9d6b0abf5be0d5d04 Mon Sep 17 00:00:00 2001 From: liuyubobobo Date: Sun, 16 Sep 2018 23:28:14 -0700 Subject: [PATCH] 0042 solved. --- .../cpp-0042/CMakeLists.txt | 7 ++ 0042-Trapping-Rain-Water/cpp-0042/main.cpp | 50 ++++++++++++++ 0042-Trapping-Rain-Water/cpp-0042/main2.cpp | 68 +++++++++++++++++++ 0042-Trapping-Rain-Water/cpp-0042/main3.cpp | 53 +++++++++++++++ 0042-Trapping-Rain-Water/cpp-0042/main4.cpp | 57 ++++++++++++++++ 0042-Trapping-Rain-Water/cpp-0042/main5.cpp | 60 ++++++++++++++++ readme.md | 2 + 7 files changed, 297 insertions(+) create mode 100644 0042-Trapping-Rain-Water/cpp-0042/CMakeLists.txt create mode 100644 0042-Trapping-Rain-Water/cpp-0042/main.cpp create mode 100644 0042-Trapping-Rain-Water/cpp-0042/main2.cpp create mode 100644 0042-Trapping-Rain-Water/cpp-0042/main3.cpp create mode 100644 0042-Trapping-Rain-Water/cpp-0042/main4.cpp create mode 100644 0042-Trapping-Rain-Water/cpp-0042/main5.cpp diff --git a/0042-Trapping-Rain-Water/cpp-0042/CMakeLists.txt b/0042-Trapping-Rain-Water/cpp-0042/CMakeLists.txt new file mode 100644 index 00000000..869e3771 --- /dev/null +++ b/0042-Trapping-Rain-Water/cpp-0042/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.5) +project(cpp_0042) + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") + +set(SOURCE_FILES main5.cpp) +add_executable(cpp_0042 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0042-Trapping-Rain-Water/cpp-0042/main.cpp b/0042-Trapping-Rain-Water/cpp-0042/main.cpp new file mode 100644 index 00000000..adac8231 --- /dev/null +++ b/0042-Trapping-Rain-Water/cpp-0042/main.cpp @@ -0,0 +1,50 @@ +/// Source : https://leetcode.com/problems/trapping-rain-water/description/ +/// Author : liuyubobobo +/// Time : 2018-09-16 + +#include +#include + +using namespace std; + + +/// Brute Force +/// Time Complexity: O(n^2) +/// Space Complexity: O(1) +class Solution { +public: + int trap(vector& height) { + + if(height.size() <= 2) + return 0; + + int res = 0; + for(int i = 1; i < height.size() - 1; i ++) { + int lmax = height[i], rmax = height[i]; + for (int l = i - 1; l >= 0; l --) + lmax = max(lmax, height[l]); + for(int r = i + 1; r < height.size(); r ++) + rmax = max(rmax, height[r]); + res += min(lmax, rmax) - height[i]; + } + return res; + } +}; + + +int main() { + + vector height1 = {0,1,0,2,1,0,1,3,2,1,2,1}; + cout << Solution().trap(height1) << endl; + // 6 + + vector height2 = {4,2,3}; + cout << Solution().trap(height2) << endl; + // 1 + + vector height3 = {4, 2, 0, 3, 2, 5}; + cout << Solution().trap(height3) << endl; + // 9 + + return 0; +} \ No newline at end of file diff --git a/0042-Trapping-Rain-Water/cpp-0042/main2.cpp b/0042-Trapping-Rain-Water/cpp-0042/main2.cpp new file mode 100644 index 00000000..8f6f8673 --- /dev/null +++ b/0042-Trapping-Rain-Water/cpp-0042/main2.cpp @@ -0,0 +1,68 @@ +/// Source : https://leetcode.com/problems/trapping-rain-water/description/ +/// Author : liuyubobobo +/// Time : 2018-09-16 + +#include +#include + +using namespace std; + + +/// Memory Search +/// +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int trap(vector& height) { + + if(height.size() <= 2) + return 0; + + vector rdp(height.size(), -1); + getR(0, height, rdp); + + vector ldp(height.size(), -1); + getL(height.size() - 1, height, ldp); + + int res = 0; + for(int i = 0; i < height.size(); i ++) + res += min(ldp[i], rdp[i]) - height[i]; + return res; + } + +private: + int getR(int index, const vector& height, vector& rdp){ + + if(index == height.size() - 1) + return rdp[index] = height[index]; + + return rdp[index] = max(height[index], getR(index + 1, height, rdp)); + } + + int getL(int index, const vector& height, vector& ldp){ + + if(index == 0) + return ldp[index] = height[index]; + + return ldp[index] = max(height[index], getL(index - 1, height, ldp)); + } +}; + + +int main() { + + vector height1 = {0,1,0,2,1,0,1,3,2,1,2,1}; + cout << Solution().trap(height1) << endl; + // 6 + + vector height2 = {4,2,3}; + cout << Solution().trap(height2) << endl; + // 1 + + vector height3 = {4, 2, 0, 3, 2, 5}; + cout << Solution().trap(height3) << endl; + // 9 + + return 0; +} \ No newline at end of file diff --git a/0042-Trapping-Rain-Water/cpp-0042/main3.cpp b/0042-Trapping-Rain-Water/cpp-0042/main3.cpp new file mode 100644 index 00000000..e1d12320 --- /dev/null +++ b/0042-Trapping-Rain-Water/cpp-0042/main3.cpp @@ -0,0 +1,53 @@ +/// Source : https://leetcode.com/problems/trapping-rain-water/description/ +/// Author : liuyubobobo +/// Time : 2018-09-16 + +#include +#include + +using namespace std; + + +/// Dynamic Programming +/// +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int trap(vector& height) { + + if(height.size() <= 2) + return 0; + + vector rdp(height.size(), height.back()); + for(int i = height.size() - 2; i >= 0; i --) + rdp[i] = max(height[i], rdp[i + 1]); + + vector ldp(height.size(), height[0]); + for(int i = 1; i < height.size(); i ++) + ldp[i] = max(height[i], ldp[i - 1]); + + int res = 0; + for(int i = 0; i < height.size(); i ++) + res += min(ldp[i], rdp[i]) - height[i]; + return res; + } +}; + + +int main() { + + vector height1 = {0,1,0,2,1,0,1,3,2,1,2,1}; + cout << Solution().trap(height1) << endl; + // 6 + + vector height2 = {4,2,3}; + cout << Solution().trap(height2) << endl; + // 1 + + vector height3 = {4, 2, 0, 3, 2, 5}; + cout << Solution().trap(height3) << endl; + // 9 + + return 0; +} \ No newline at end of file diff --git a/0042-Trapping-Rain-Water/cpp-0042/main4.cpp b/0042-Trapping-Rain-Water/cpp-0042/main4.cpp new file mode 100644 index 00000000..9375463a --- /dev/null +++ b/0042-Trapping-Rain-Water/cpp-0042/main4.cpp @@ -0,0 +1,57 @@ +/// Source : https://leetcode.com/problems/trapping-rain-water/description/ +/// Author : liuyubobobo +/// Time : 2018-09-16 + +#include +#include + +using namespace std; + + +/// Using Stack +/// +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int trap(vector& height) { + + if(height.size() <= 2) + return 0; + + vector stack; + int res = 0; + for(int i = 0; i < height.size(); i ++){ + while(!stack.empty() && height[i] > height[stack.back()]){ + int cur = stack.back(); + stack.pop_back(); + if(stack.empty()) + break; + + int dis = i - stack.back() - 1; + int h = min(height[stack.back()], height[i]) - height[cur]; + res += h * dis; + } + stack.push_back(i); + } + return res; + } +}; + + +int main() { + + vector height1 = {0,1,0,2,1,0,1,3,2,1,2,1}; + cout << Solution().trap(height1) << endl; + // 6 + + vector height2 = {4,2,3}; + cout << Solution().trap(height2) << endl; + // 1 + + vector height3 = {4, 2, 0, 3, 2, 5}; + cout << Solution().trap(height3) << endl; + // 9 + + return 0; +} \ No newline at end of file diff --git a/0042-Trapping-Rain-Water/cpp-0042/main5.cpp b/0042-Trapping-Rain-Water/cpp-0042/main5.cpp new file mode 100644 index 00000000..f39be25b --- /dev/null +++ b/0042-Trapping-Rain-Water/cpp-0042/main5.cpp @@ -0,0 +1,60 @@ +/// Source : https://leetcode.com/problems/trapping-rain-water/description/ +/// Author : liuyubobobo +/// Time : 2018-09-16 + +#include +#include + +using namespace std; + + +/// Two Pointers +/// +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int trap(vector& height) { + + if(height.size() <= 2) + return 0; + + int res = 0; + int l = 0, r = height.size() - 1, lmax_i = l, rmax_i = r; + while(l < r){ + if(height[l] < height[r]){ + l ++; + if(height[l] < height[lmax_i]) + res += min(height[lmax_i], height[rmax_i]) - height[l]; + else + lmax_i = l; + } + else{ + r --; + if(height[r] < height[rmax_i]) + res += min(height[lmax_i], height[rmax_i]) - height[r]; + else + rmax_i = r; + } + } + return res; + } +}; + + +int main() { + + vector height1 = {0,1,0,2,1,0,1,3,2,1,2,1}; + cout << Solution().trap(height1) << endl; + // 6 + + vector height2 = {4,2,3}; + cout << Solution().trap(height2) << endl; + // 1 + + vector height3 = {4, 2, 0, 3, 2, 5}; + cout << Solution().trap(height3) << endl; + // 9 + + return 0; +} \ No newline at end of file diff --git a/readme.md b/readme.md index 5a9a583b..e2927421 100644 --- a/readme.md +++ b/readme.md @@ -80,6 +80,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 039 | [Combination Sum](https://leetcode.com/problems/combination-sum/description/) | [无] | [C++](0039-Combination-Sum/cpp-0039/) | | | | 040 | [Combination Sum II](https://leetcode.com/problems/combination-sum-ii/description/) | [无] | [C++](0040-Combination-Sum-II/cpp-0040/) | | | | | | | | | | +| 042 | [Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/description/) | [solution](https://leetcode.com/problems/trapping-rain-water/solution/) | [C++](0042-Trapping-Rain-Water/cpp-0042/) | | | +| | | | | | | | 046 | [Permutations](https://leetcode.com/problems/permutations/description/) | [缺:排列算法整理] | [C++](0046-Permutations/cpp-0046/) | [Java](0046-Permutations/java-0046/src/) | | | 047 | [Permutations II](https://leetcode.com/problems/permutations-ii/description/) | [无] | [C++](0047-Permutations-II/cpp-0047/) | | | | 048 | [Rotate Image](https://leetcode.com/problems/rotate-image/description/) | [无] | [C++](0048-Rotate-Image/cpp-0048/) | | |