惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
IT之家
IT之家
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
WordPress大学
WordPress大学
N
Netflix TechBlog - Medium
阮一峰的网络日志
阮一峰的网络日志
P
Proofpoint News Feed
L
LangChain Blog
博客园 - Franky
美团技术团队
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
博客园 - 叶小钗
小众软件
小众软件
Y
Y Combinator Blog
B
Blog RSS Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
Docker
Hugging Face - Blog
Hugging Face - Blog
Jina AI
Jina AI
罗磊的独立博客
大猫的无限游戏
大猫的无限游戏
Vercel News
Vercel News

博客园 - guolongnv

useful Claude code skills & plugins bell in bash Anaconda usage tips python编程提升1(答案篇) python编程提升1(问题篇) How p4 get authorized How to format a jason file in vim How to open utfb8 in linux env json 文件查看和编辑 How to change the color display of directories in putty? Screen frequency used commamd How to load yaml file in python How to get IPv4 address Basic mysql command 4. Median of Two Sorted Arrays 3.lengthOfLongestSubstring 1.two sum pandas教程 How to make mail more effectively?
42.trapping-rain-water
guolongnv · 2021-08-04 · via 博客园 - guolongnv

Question:

 https://leetcode.com/problems/trapping-rain-water/

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.

Example 1:

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.

Example 2:

Input: height = [4,2,0,3,2,5]
Output: 9

Constraints:

  • n == height.length
  • 0 <= n <= 2 * 104
  • 0 <= height[i] <= 105

Answer:

class Solution(object):
    def trap(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        left=[]
        right=[]
        left_max=0
        right_max=0
        for x in height:
               left_max=max(x,left_max)
               left.append(left_max)
        height.reverse()
        for y in height:
               right_max=max(y,right_max)
               right.append(right_max)
        right.reverse()
        output = 0
        for i in range(len(height)):
            output += min(right[i],left[i]) - height[i]
        return output