break algorithm---接雨水类问题


😡😡😡


声明:
算法基于https://labuladong.github.io/
同步git地址:https://github.com/muxintong/break-algorithm/
python语言实现


11.container-with-most-water(left-right_pointer)
42.trapping-rain-water


😡😡😡



11.container-with-most-water(left-right_pointer)

https://labuladong.github.io/algo/4/31/129/
https://leetcode.com/problems/container-with-most-water/


from typing import List


class Solution:
    def maxArea(self, height: List[int]) -> int:
        # 左右指针l,r分别指向头尾,二者相向移动
        l = 0
        r = len(height) - 1

        res = 0
        while l < r:
            s = min(height[l], height[r]) * (r - l)
            res = max(res, s)
            # NOTE:左右指针的移动,移动指针高度小的,
            # 因为是求面积的最大值,移动高度小的才有可能使该高度变大,进而增大面积
            if height[l] < height[r]:
                l += 1
            else:
                r -= 1

        return res


def main():
    # Input: height = [1,8,6,2,5,4,8,3,7]
    # Output: 49
    solution1 = Solution()
    print(solution1.maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7]))

    # Input: height = [1,1]
    # Output: 1
    solution2 = Solution()
    print(solution2.maxArea([1,1]))


if __name__ == '__main__':
    main()


😡😡😡



42.trapping-rain-water

https://labuladong.github.io/algo/4/31/129/
https://leetcode.com/problems/trapping-rain-water/

from typing import List


class Solution:
    def trap(self, height: List[int]) -> int:
        """
        对于柱i,其所能容纳的最大雨水量为:
        min(l_max[0-i], r_max[i,n] - height[i]
        """
        n = len(height)
        l_max_memo = [0] * n
        r_max_memo = [0] * n

        l_max = height[0]
        r_max = height[n - 1]

        for i in range(1, n - 1, 1):
            l_max = max(l_max, height[i - 1])
            l_max_memo[i] = l_max

        for i in range(n - 2, 0, -1):
            r_max = max(r_max, height[i + 1])
            r_max_memo[i] = r_max

        res = 0
        for i in range(1, n - 1):
            # NOTE:所接雨水量不能为负值,只有当其大于0时才将其计入答案中
            temp = min(l_max_memo[i], r_max_memo[i]) - height[i]
            if temp > 0:
                res += temp

        return res


    # 备忘录解法
    def trap2(self, height: List[int]) -> int:
        """
        对于柱i,其所能容纳的最大雨水量为:
        min(l_max[0-i], r_max[i,n] - height[i]
        """
        n = len(height)
        # 使用数组作为备忘录
        l_max_memo = [0] * n
        r_max_memo = [0] * n

        # base case
        l_max_memo[0] = height[0]
        r_max_memo[n - 1] = height[n - 1]

        # l_max:左至右
        for i in range(1, n - 1, 1):
            l_max_memo[i] = max(height[i], l_max_memo[i - 1])

        # r_max:右至左
        for i in range(n - 2, 0, -1):
            r_max_memo[i] = max(height[i], r_max_memo[i + 1])

        res = 0
        for i in range(1, n - 1):
            # NOTE:所接雨水量不能为负值,只有当其大于0时才将其计入答案中
            temp = min(l_max_memo[i], r_max_memo[i]) - height[i]
            if temp > 0:
                res += temp

        return res


def main():
    # Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
    # Output: 6
    # Explanation: The above elevation map (black section) is represented by
    # array [0,1,0,2,1,0,1,3,2,1,2,1].
    # In this case, 6 units of rain water (blue section) are being trapped.
    solution1 = Solution()
    print(solution1.trap([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]))

    # Input: height = [4,2,0,3,2,5]
    # Output: 9
    solution2 = Solution()
    print(solution2.trap2([4, 2, 0, 3, 2, 5]))


if __name__ == '__main__':
    main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

killingwill

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值