forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
21d4b63
commit 0c118cb
Showing
7 changed files
with
297 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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/) | | | | ||
|