LeetCode 接雨水 题目来源:https://leetcode-cn.com/problems/trapping-rain-water/ 今天在LeetCode上看到一道很有意思的题目:接雨水。题目给出了一些柱子的高度,然后要求出这些柱子可以装的水有多少。 我想到的方法是从左往右遍历数组,记录当前的最高的柱子高度,只要遇到比最高柱子低的柱子,就假设这个
Leetcode 42题 接雨水(Trapping Rain Water) https://leetcode-cn.com/problems/trapping-rain-water/ 题目内容 给定n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。 示例图 示例: 输入: [0,1,0,2,1,0,1,3,2,1,2,1] 输出: 6 思路 我们可以设置两个数组...
想到这两中情形的话就好理解多了。 还有Approach 4: Using 2 pointers方法,详细见:https://leetcode.com/problems/trapping-rain-water/solution/
result +=max(0,min(h, maxh[i]) - height[i])returnresultif__name__ =="__main__":assertSolution().trap([0,1,0,2,1,0,1,3,2,1,2,1]) ==6 欢迎查看我的Github(https://github.com/gavinfish/LeetCode-Python) 来获得相关源代码。
## LeetCode 42H - Trapping Rain Water,双指针法 from typing import List class Solution: ## height 表示列表中元素的取值,高度 def trap(self, height: List[int]) -> int: if not height: ## 如果没有任何高度 return 0 ## 接不住任何雨水 ## 初始化左右指针 l, r = 0, len(height) - 1...
https://leetcode.cn/problems/trapping-rain-water/description/ 给定n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。 这道题如果难的话就是难在思路,怎么去算 三、终版 o(n) 找到最高点,两边依次以最高边为终边,看自己是否有积水,是否有积水取决于历史是否有高...
Enter array elements: 1 2 3 4 5 0 Enter size of array: 5 Enter array elements: 5 4 3 2 1 0 Enter size of array: 6 Enter array elements: 9 2 5 2 4 1 5 Problem reference:https://leetcode.com/problems/trapping-rain-water/...
https://leetcode-cn.com/problems/trapping-rain-water/ 题目内容 给定n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。 示例: 输入: [0,1,0,2,1,0,1,3,2,1,2,1] 输出: 6 思路 我们可以设置两个数组,「left_height_array」和「right_height_array」; ...
Trapping Rain Water 题解 题目描述 即现实中的地面积水问题。抽象出来即从某个序列Seq中提取出先单调递减后单调递增的子序列集合(集合中子序列只允许头尾存在重复),求此集合中所有子序列s(长为d)的"凹陷值"((d - 2) * min{s[0],s[d-1]} - (sum(s) - s[0] - s[d-1]))之和。如:[0,1,0...
LeetCode42 Trapping Rain Water this problem is really classic and we have a really classic way to solve this problem: we iterate every value of given array, and each time, for a fixed value, we only consider the water it can contained, and we maintained two variables: leftMax and right...