Skip to content

Commit

Permalink
0042 solved.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyubobobo committed Sep 17, 2018
1 parent 21d4b63 commit 0c118cb
Show file tree
Hide file tree
Showing 7 changed files with 297 additions and 0 deletions.
7 changes: 7 additions & 0 deletions 0042-Trapping-Rain-Water/cpp-0042/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})
50 changes: 50 additions & 0 deletions 0042-Trapping-Rain-Water/cpp-0042/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/// Source : https://leetcode.com/problems/trapping-rain-water/description/
/// Author : liuyubobobo
/// Time : 2018-09-16

#include <iostream>
#include <vector>

using namespace std;


/// Brute Force
/// Time Complexity: O(n^2)
/// Space Complexity: O(1)
class Solution {
public:
int trap(vector<int>& 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<int> height1 = {0,1,0,2,1,0,1,3,2,1,2,1};
cout << Solution().trap(height1) << endl;
// 6

vector<int> height2 = {4,2,3};
cout << Solution().trap(height2) << endl;
// 1

vector<int> height3 = {4, 2, 0, 3, 2, 5};
cout << Solution().trap(height3) << endl;
// 9

return 0;
}
68 changes: 68 additions & 0 deletions 0042-Trapping-Rain-Water/cpp-0042/main2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/// Source : https://leetcode.com/problems/trapping-rain-water/description/
/// Author : liuyubobobo
/// Time : 2018-09-16

#include <iostream>
#include <vector>

using namespace std;


/// Memory Search
///
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
int trap(vector<int>& height) {

if(height.size() <= 2)
return 0;

vector<int> rdp(height.size(), -1);
getR(0, height, rdp);

vector<int> 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<int>& height, vector<int>& 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<int>& height, vector<int>& ldp){

if(index == 0)
return ldp[index] = height[index];

return ldp[index] = max(height[index], getL(index - 1, height, ldp));
}
};


int main() {

vector<int> height1 = {0,1,0,2,1,0,1,3,2,1,2,1};
cout << Solution().trap(height1) << endl;
// 6

vector<int> height2 = {4,2,3};
cout << Solution().trap(height2) << endl;
// 1

vector<int> height3 = {4, 2, 0, 3, 2, 5};
cout << Solution().trap(height3) << endl;
// 9

return 0;
}
53 changes: 53 additions & 0 deletions 0042-Trapping-Rain-Water/cpp-0042/main3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/// Source : https://leetcode.com/problems/trapping-rain-water/description/
/// Author : liuyubobobo
/// Time : 2018-09-16

#include <iostream>
#include <vector>

using namespace std;


/// Dynamic Programming
///
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
int trap(vector<int>& height) {

if(height.size() <= 2)
return 0;

vector<int> rdp(height.size(), height.back());
for(int i = height.size() - 2; i >= 0; i --)
rdp[i] = max(height[i], rdp[i + 1]);

vector<int> 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<int> height1 = {0,1,0,2,1,0,1,3,2,1,2,1};
cout << Solution().trap(height1) << endl;
// 6

vector<int> height2 = {4,2,3};
cout << Solution().trap(height2) << endl;
// 1

vector<int> height3 = {4, 2, 0, 3, 2, 5};
cout << Solution().trap(height3) << endl;
// 9

return 0;
}
57 changes: 57 additions & 0 deletions 0042-Trapping-Rain-Water/cpp-0042/main4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/// Source : https://leetcode.com/problems/trapping-rain-water/description/
/// Author : liuyubobobo
/// Time : 2018-09-16

#include <iostream>
#include <vector>

using namespace std;


/// Using Stack
///
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
int trap(vector<int>& height) {

if(height.size() <= 2)
return 0;

vector<int> 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<int> height1 = {0,1,0,2,1,0,1,3,2,1,2,1};
cout << Solution().trap(height1) << endl;
// 6

vector<int> height2 = {4,2,3};
cout << Solution().trap(height2) << endl;
// 1

vector<int> height3 = {4, 2, 0, 3, 2, 5};
cout << Solution().trap(height3) << endl;
// 9

return 0;
}
60 changes: 60 additions & 0 deletions 0042-Trapping-Rain-Water/cpp-0042/main5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/// Source : https://leetcode.com/problems/trapping-rain-water/description/
/// Author : liuyubobobo
/// Time : 2018-09-16

#include <iostream>
#include <vector>

using namespace std;


/// Two Pointers
///
/// Time Complexity: O(n)
/// Space Complexity: O(1)
class Solution {
public:
int trap(vector<int>& 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<int> height1 = {0,1,0,2,1,0,1,3,2,1,2,1};
cout << Solution().trap(height1) << endl;
// 6

vector<int> height2 = {4,2,3};
cout << Solution().trap(height2) << endl;
// 1

vector<int> height3 = {4, 2, 0, 3, 2, 5};
cout << Solution().trap(height3) << endl;
// 9

return 0;
}
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ email: [[email protected]](mailto:[email protected])
| 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/) | | |
Expand Down

0 comments on commit 0c118cb

Please sign in to comment.