leetcode $42 in cpp
2016-05-27 04:03
399 查看
Code: (This method is not the best one. There are other methods using two pointers)
Key: a bar could be a left bound if it is no less than its right. A bar could be a right bound if it is no less than its left.
Key: a bar could be a left bound if it is no less than its right. A bar could be a right bound if it is no less than its left.
class Solution { public: int trap(vector<int>& height) { if(height.empty()) return 0; return find(height,0, height.size() - 1); } int find(vector<int> height,int left, int right){ if(left >=right - 1) return 0; //find the left bound and the next closest right bound int left_bound = -1;//next left bound that could hold the water int right_bound = -1;//next closest right bound that could form a trap with left_bound for(int i = left; i < height.size(); i ++){//find the left bound that could hold water //two cases that it could not be a left bound: //1. if height < 0 //2. if its right is no less than it if(height[i] <= 0 || ( i + 1 < height.size() && height[i + 1] >= height[i])) continue; left_bound = i; break; } if(left_bound == -1 || left_bound >= height.size() - 2) return 0; int right_bound_height = -1; ////find the next closest right bound that could hold water: //during the iterations we need to find: //1. temporary right bound candidates that could trap water with the left bound(rule: it is no // less than its right) //2. the true right bound which is the highest bound among the right bound candidates(The // height of the true right bound among the candidates could be no less than or less than // the height of the left bound) // Briefly, if we find a right bound with height > height of leftbound, we immediately stop // if we could not find a right bound with height > height of leftbound, we choose the // tallst right bound among the right bound candidates found in 1. for(int i = left_bound + 1; i <= right; i++){ //three cases that it could not be a right bound of a trap: //1. if its height is <= 0 //2. if its left is no less than it //3. if it is shorter than the current hightest right bound if(height[i] <=0 || (i - 1 >= 0 && height[i-1] >=height[i]) || right_bound_height > height[i]) continue; right_bound = i; right_bound_height = height[i]; if(right_bound_height >= height[left_bound]) break;//once we find a right bound taller //than the left bound, we terminate the loops. } if(right_bound == -1 || right_bound <= left_bound + 1) return 0;//cannot find right bound //calculate the water trapped int res = right_bound - left_bound - 1; int min_height = height[right_bound]>height[left_bound]?height[left_bound]:height[right_bound]; res*= min_height; //minus the volumes of barriers in the trap for(int i = left_bound + 1; i < right_bound; i ++){ res -= height[i] > min_height?min_height:height[i]; } //calculate the next pair of left bound and right bound. return res + find(height, right_bound, right); } };
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- C++中const用法总结
- CPP 虚函数、虚函数表及虚拟继承(转)
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- Deploying Control Plane Policing
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解