美文网首页
2018-12-20

2018-12-20

作者: 怎样会更好 | 来源:发表于2018-12-20 16:14 被阅读0次

    雨水收集:
    思路每一列的水量就是他左右最高高度的较小值减去自己的高度

    public int trap(int[] height) {
            int res = 0, l = 0, r = height.length - 1;
            while (l < r) {
                int mn = Math.min(height[l], height[r]);
                if (height[l] == mn) {
                    ++l;
                    while (l < r && height[l] < mn) {
                        res += mn - height[l++];
                    }
                } else {
                    --r;
                    while (l < r && height[r] < mn) {
                        res += mn - height[r--];
                    }
                }
            }
            return res;
        }
    

    相关文章

      网友评论

          本文标题:2018-12-20

          本文链接:https://www.haomeiwen.com/subject/choykqtx.html