博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Leetcode】Container With Most Water
阅读量:4187 次
发布时间:2019-05-26

本文共 920 字,大约阅读时间需要 3 分钟。

/***********************************************************************************

Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai).
n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0).
Find two lines, which together with x-axis forms a container, such that the container contains the most water.
*************************************************************************************/

解析:题目意思很简单,就是选择两条线,使其与x轴围成的矩形面积最大。

应该想到用夹逼的方法进行处理。如果左边的数值小于右边,则向右移动,否则向左移动,同时记录最大的面积值。

完整AC如下:

class Solution {public:    int maxArea(vector
& height) { int water = 0; int i = 0, j = height.size() - 1; while (i < j) { int h = min(height[i], height[j]); water = max(water, (j - i) * h); while (height[i] <= h && i < j) i++; //find greater h while (height[j] <= h && i < j) j--; } return water;}};

转载地址:http://bodoi.baihongyu.com/

你可能感兴趣的文章
glitch free 时钟切换逻辑的实现
查看>>
用ncverilog跑仿真时,如何去除对特定路径的timing检查
查看>>
在ncverilog仿真条件设置中+nospecify ,+notimingcheck 和 +delay_mode_zero之间有什么区别
查看>>
如何编写Xilinx ISE环境下的综合约束文件ucf
查看>>
xilinx iMpact 923 warning信息的解决 (can not find cable,check cable setup!)
查看>>
iphone掉水里后的处理方法
查看>>
linux下nerdtree安装方法
查看>>
TCL中有关regexp匹配表达式的说明
查看>>
scandef格式详细说明
查看>>
gvim使用指南(学好就可下山了)
查看>>
Halide入门教程第2讲:处理图像
查看>>
Halide入门第3讲:如何设置环境变量以检查llvm编译生成的代码
查看>>
git 更新和冲突解决简单流程
查看>>
Lattice FPGA 使用指南1 - clarity designer出现ERROR – Error trying to create component 错误的解决办法
查看>>
verdi加载vhdl和verilog混合RTL设计的方法
查看>>
verdi编译vhdl文件时,报出"warning:*Warn* Unknown argument –vhdl08"的解决 办法
查看>>
linux 常见命令总结
查看>>
信号差分对的优势说明
查看>>
verilog timescale的两种仿真处理方法
查看>>
lattice FPGA 使用指南2 - DDR3 sdram controller IP配置注意事项
查看>>