【LeetCode】11. Container With Most Water
2024-08-28 16:48:34
题目:
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.
Note: You may not slant the container.
翻译:
给定n个非负整数a1,a2,...,an,其中每个代表一个点坐标(i,ai)。 n个垂直线段例如线段的两个端点在(i,ai)和(i,0)。 找到两个线段,与x轴形成一个容器,使其包含最多的水。
备注:不考虑倾斜容器。
思路:容积其实就是面积,我们都知道长方形的面积=长*宽,不妨我们就从长最长的长方形找起,即令left = 0, right = height.size() - 1,但是在找下一个长方形时,长肯定会变短,要弥补这一段损失就必须加宽宽度,所以一下个就换掉两条宽中较小的那一个。
public class Solution {
public int maxArea(int[] height) {
int left=0;
int right=height.length-1;
int maxArea=0;
while(left<right){
maxArea=Math.max(maxArea,Math.min(height[left],height[right])*(right-left));
if(height[left]>height[right]){
right--;
}else{
left++;
}
}
return maxArea;
}
}
最新文章
- PostgreSQL隐藏字段tableoid
- C++的ORM工具比较
- Git Server &; Git Hook
- 【MySQL】binlog_format以及binlog事务记录分析
- **bootstrap常见常用样式总结
- 结构型模式——Adapter
- loadrunner_analysis技巧_filter和group by
- IIS6,IIS7中查看w3wp进程
- 【jQuery、原生】键盘键入两位小数
- JAVA基础--继承和权限控制
- Java中for_each循环的使用
- 【深度学习系列】用PaddlePaddle进行车牌识别(二)
- vue源码阅读(一)
- web@css高级选择器(after,befor用法),基本css样式
- [PHP] 算法-二叉树的子结构判断的PHP实现
- RabbitMQ 发布订阅-实现延时重试队列(参考)
- 两个简单的动态规划问题,0-1背包和最大不相邻数累加和,附递归c代码
- ubuntu 发送邮件
- Find non-overlap jobs with max cost
- aarch64_m2
热门文章
- 剑指offer系列28--字符流中第一个不重复的字符
- CSS媒体查询(@media)
- html body标签的几个属性 禁用鼠标右键,禁用鼠标选中文字等
- Exception error message with incorrect line number
- VS 开发工具中的Remote Debug 功能远程调试程序经验分享
- ADF_Starting系列4_使用ADF开发富Web应用程序之维护User Interface(Part1)
- PLSQL_性能优化系列19_Oracle Explain Plan解析计划通过Profile绑定
- CF 161D Distance in Tree 树形DP
- vs2012出现无法启动iis express web 服务器的错误
- ylbtech-dbs:ylbtech-6,record(生活记录)