本文主要是介绍[leetcode刷题系列]Trapping Rain Water,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- - 嗯, 水题
const int MAXN = 1e6 + 10;int dleft[MAXN], dright[MAXN];class Solution {
public:int trap(int A[], int n) {// Start typing your C/C++ solution below// DO NOT write int main() functionif(n <= 0)return 0;dleft[0] = 0;for(int i = 1; i < n; ++ i)dleft[i] = max(dleft[i - 1], A[i - 1]);dright[n - 1] = 0;for(int i = n - 2; i >= 0; -- i)dright[i] = max(dright[i + 1], A[i + 1]);int ans = 0;for(int i = 0; i < n; ++ i){int cur = min(dleft[i],dright[i]);if(cur > A[i])ans += cur - A[i];}return ans;}
};
这篇关于[leetcode刷题系列]Trapping Rain Water的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!