42. Trapping Rain Water leetcode Python 2016 new season

STM32 HAL库定时器中断实现步进电机S型加减速控制与脉冲精准计数 本文详细介绍了如何使用STM32 HAL库定时器中断实现步进电机的S型加减速控制与脉冲精准计数。通过CubeMX配置定时器、计算PSC和ARR值,并结合S型曲线算法,实现平滑启停和精准定位。文章还提供了脉冲计数方案和常见问题排查技巧,帮助开发者优化步进电机控制性能。 阅读详情

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

For example, 
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.


The above elevation map 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. Thanks Marcos for contributing this image!

Show Tags
Show Similar Problems


class Solution(object):
    def trap(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        if not height:
            return 0
        height_len = len(height)
        maxLeft = [0 for _ in range(height_len)]
        maxRight = [0 for _ in range(height_len)]
        max_value = height[0]
        for index in range(height_len):
            maxLeft[index] = max_value
            if max_value < height[index]:
                max_value = height[index]
        max_value = height[height_len - 1]
        for index in reversed(range(height_len)):
            maxRight[index] = max_value
            if max_value < height[index]:
                max_value = height[index]
        result = 0
        for index in range(height_len):
            water = min(maxRight[index], maxLeft[index]) - height[index]
            if water > 0:
                result += water
        return result
                









CarPlay盒子选购避坑指南:为什么你的车机破解了却用不了?从USB设备ID说起 本文深入解析CarPlay盒子硬件兼容性问题,从USB设备ID匹配到车机适配逻辑,揭示破解成功却无法使用的根本原因。通过分析USB设备识别的核心要素和逆向工程usb_device_manager.xml文件,提供三种主流解决方案对比,并强调操作中的硬件保护机制,帮助车主避免常见陷阱。 阅读详情

相关推荐

Python与西门子PLC数据交互实战:基于Snap7的跨平台工业通信指南

本文详细介绍了如何使用Python和Snap7库实现与西门子PLC的数据交互。内容涵盖跨平台环境搭建、连接参数配置、数据读写实战及性能优化技巧,为工业自动化、数据采集和智能制造场景提供了一套轻量、灵活且高效的通信解决方案。

vim8coder的博客 282

LeetCode第四十二题—Python实现

title: LeetCode No.42 categories: OJ LeetCode tags: Programing LeetCode OJ LeetCode第四十二题 题目描述 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。 示例 1: 输入:height = [0,1,0,2,1,0,1,3,2,1,2,1] 输出:6 解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,.

StriveZs'Blog 333

计算机视觉之姿态识别(原理+代码实操)

计算机视觉中的姿态识别原理+模型介绍,同时通过代码实操简单的演示基于mediapipe的单人姿态识别。

输出既学习 3万+

Python | Leetcode Python题解之第42题接雨水

Python | Leetcode Python题解之第42题接雨水

Mopes__的博客 533

sdio wifi 移植

介绍 简单来说,sdio起源自sd标准,顾名思义就是sd的IO接口(interface)的意思,更具体的说明,sd本来是记忆卡的标准,但是现在也可以把SD卡拿来插上一些外围设备接口使用,这个就是就叫做sdio。 而且sdio本身是一种相当单纯的技术,通过sd的IO引脚来连接外部外围,并且通过sd的IO数据接位与外围传输数据,而且sd协会也推出很完整的sdio stack 驱动程序,使得sdio外围得开发与应用变得非常热门 这里我们要移植的是RTL8822cs的sdio WiFi 模块,将其移植到rk3

Enosji的博客 9609

一种车内UWB圆极化天线的理论推导及具体实现

本文详细描述了一款工作于UWB channel 9也即中心频率为7987.2MHz,带宽为499.2MHz的圆极化天线。首先是理论推导圆极化天线的物理尺寸参数,分析物理尺寸参数对天线性能指标的影响。然后分析圆极化天线在汽车驾驶室这种狭窄空间应用中带来的优势,也即可以最大化的极化损失多径,提高主径接收信号的信噪比。

weixin_44555637的博客 2961

【Leetcode | Python】42. 接雨水 [栈][数组][双指针][动态规划][单调栈]

对于每一个柱子,它所能接到的雨水量并不取决于它相邻左右两侧的柱子高度,而取决于它【左右两侧的最高的柱子高度】; 类似于 11. 盛最多水的容器,由于“短板效应”,每个柱子的接水量取决于它左右两侧最高的柱子中较矮的那一个; 使用对向双指针,遍历的同时,完成: 判断哪边是短板; 根据短板计算当前柱子的接水量; 将短板向内移动; 更新当前左右两侧最高的柱子高度。

weixin_44342460的博客 934

EDKII实战:手把手调试PciHostBridge.c,看UEFI如何分配你的PCIe设备资源(IO/Memory空间)

本文深入解析EDKII中PciHostBridge.c模块的工作原理,详细指导如何调试PCIe设备资源分配(IO/Memory空间)。通过实战案例展示UEFI环境下Root Bridge初始化、地址空间转换及协议安装等核心流程,提供常见问题排查方法与性能优化建议,帮助开发者解决PCIe设备兼容性问题。

weixin_29201933的博客 208

Leetcode题42、接雨水(Python题解)字节跳动面试题

问题:给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。 示例: 输入: [0,1,0,2,1,0,1,3,2,1,2,1] 输出: 6 题目来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/trapping-rain-water 难度:困难 分析:非常有意思的一道题,通过左右结点值来定义中凹处的值。 解决方法: 1:暴力解法,遍历 这个解法很好理解,每个位置的储水量由它左边最大数和右边最大数的最小值决定

AI414010的博客 1237

Win10 环境下 ISE14.7 iMPACT 闪退的 DLL 修复方案 - FPGA 开发实战

本文详细介绍了在Win10系统下解决ISE14.7 iMPACT工具闪退问题的DLL修复方案,特别针对FPGA开发中常见的兼容性问题。通过替换关键DLL文件,有效解决了iMPACT在生成PROM文件时的崩溃问题,并提供了详细的步骤说明和注意事项,帮助开发者提升工作效率。

weixin_29197051的博客 209

OpenHarmony Stage模型深度解剖:从Ability Kit到沙箱隔离的全链路底层原理

基于微内核架构,提供进程管理、内存管理、IPC通信等基础能力:包含V8引擎、ArkTS运行时、渲染引擎等核心组件:Ability Kit框架,提供应用生命周期管理、组件调度等能力:基于Stage模型的应用程序。

m0_67505153的博客 191

LeetCode刷题(python版)——Topic42接雨水

给定n个非负整数表示每个宽度为1的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。

weixin_54039182的博客 1577

Python爬虫实战:模拟登录知乎 – 通过加密参数和验证码实现自动登录

通过本篇博客的讲解,我们学习了如何通过 POST请求 和 验证码识别,模拟登录知乎并爬取用户信息、问题数据。随着爬虫技术的发展,面对更复杂的反爬虫机制,我们可以不断调整策略,以绕过这些防护,实现数据的自动化抓取。

2201_76125261的博客 731

Leetcode 第 42 场双周赛题解(Python)

Leetcode 第 42 场双周赛题解 周赛日期:2020/12/26 题目1:5625. 比赛中的配对次数难度: 简单 给你一个整数 n ,表示比赛中的队伍数。比赛遵循一种独特的赛制: 如果当前队伍数是 偶数 ,那么每支队伍都会与另一支队伍配对。总共进行 n / 2 场比赛,且产生 n / 2 支队伍进入下一轮。 如果当前队伍数为 奇数 ,那么将会随机轮空并晋级一支队伍,其余的队伍配对。总共进行 (n - 1) / 2 场比赛,且产生 (n - 1) / 2 + 1 支队伍进入下一轮。 返..

blowfire123的博客 1083

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

LeetCode42——Trapping Rain Water

LeetCode42——Trapping Rain Water 题意: 看图就很明显。 但是做起来就没想象的简单了。。。 我最初的疑惑: 如果序列是这种情况01010,也就是说“凹点”两边都是“凸点”这样就简单了,直接根据短板原理可求,遍历求出索引为i-1和i+1的value的最小值即可。。。 但是如果遇到这种情况21012,也就是中间0的凹点,两边的壁呈递增,就懵逼了。。。

上善若水 1984

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

LeetCode-Python-42. 接雨水

给定n个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。感谢 Marcos贡献此图。 示例: 输入: [0,1,0,2,1,0,1,3,2,1,2,1] 输出: 6 思路: 每个位置上积水的高度,...

Keep Coding 1988

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

LeetCode 42 用Python3来接雨水

leetcode 42 (https://leetcode-cn.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 is able to t...

Key Board的博客 446

leetcode 42 接雨水 python

每天坚持刷题!! 题目要求 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。 题目分析 1. 首先明确如何计算一个可以接雨水的结构的面积,第一需要两个边界,类似桶的两个边,注意此时取得是两个边界的最小值为最终边界的高,然后用边界的高乘以两个边界的距离再减去两个边界之间所有柱子的面积即可。 2. 从上面的分析我们需要在遍历时寻找两个边界,同...

zy0707ok的博客 1303

LeetCode:42. Trapping Rain Water - Python

问题描述:42. 接雨水给定n个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。感谢 Marcos贡献此图。示例:输入: [0,1,0,2,1,0,1,3,2,1,2,1] 输出: 6问题分析:方法一,我们观察其中...

Growth Diary 3824

【LeetCode】42. Trapping Rain Water 解题报告(Python)

题目分析: 这个题可以先找出最大值,然后从两侧逼近最高值,以左侧为例说明:左侧值高则可蓄水(让左侧的值减去当前值),否则更新左侧值然后循环,右侧与左侧思路相同,代码已有注释,不再详细解释。 测试代码: class Solution: def trap(self, height): if height == []: return 0 max_num = max...

L141210113的专栏 915

leetcode42 接雨水(Python实现)

题目:给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 感谢 Marcos 贡献此图。 示例: 输入: [0,1,0,2,1,0,1,3,2,1,2,1] 输出: 6 来源:力扣(LeetCode) 链接:...

airen的博客 854

LeetCode--Trapping Rain Water(捕获雨水)Python

题目: 假设n个非负整数代表一个海拔图,每个条的宽度为1,计算它能在雨后捕获多少水。 For example, 例如, lì例 rú如 , Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. 给定数组[ 0,1,0,2,1,0,1,3,2,1,2,1 ],海拔图如下,返回6。 解题思路: 观

诚实的小小乐 746

LeetCode 42. 接雨水 Python实现5种算法

42. 接雨水 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 感谢 Marcos 贡献此图。 示例: 输入: [0,1,0,2,1,0,1,3,2,1,2,1] 输出: 6 动态规划(开始想到的)(O(n^2)) class Solution: def trap(self, height: List[i

buglesxu的博客 1412

【Leetcode】Python实现四数之和

给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。 注意: 答案中不可以包含重复的四元组。 示例: 给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。 满足要求的四元组集...

HelloWorld 2661

leetcode42

题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1]...

hua111hua的博客 492

leetcode第41题python版缺失的第一个正数

41. 缺失的第一个正数 给你一个未排序的整数数组 nums ,请你找出其中没有出现的最小的正整数。 请你实现时间复杂度为 O(n) 并且只使用常数级别额外空间的解决方案。

前端帅小伙 1311
上一篇: 40. Combination Sum II leetcode Python 2016 new season
下一篇: 43. Multiply Strings leetcode python 2016 new season
hyperbolechi
博客等级 码龄12年 7粉丝 178原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值