[LC] 11. Container With Most Water
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 and n is at least 2.
The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
Example:
- Input: [1,8,6,2,5,4,8,3,7]
- Output: 49
- class Solution {
- public int maxArea(int[] height) {
- int left = 0, right = height.length - 1;
- int res = 0;
- while (left < right) {
- res = Math.max(res, Math.min(height[left], height[right]) * (right - left));
- // move the smaller slant
- if (height[left] < height[right]) {
- left += 1;
- } else {
- right -= 1;
- }
- }
- return res;
- }
- }
[LC] 11. Container With Most Water的更多相关文章
- leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II
11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...
- Leetcode 11. Container With Most Water(逼近法)
11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...
- LeetCode Array Medium 11. Container With Most Water
Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...
- 刷题11. Container With Most Water
一.题目说明 11.Container With Most Water,这个题目难度是Medium. 二.我的做法 乍一看,简单啊,两个for循环就可以了,我在本地写的. #include<io ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- [leecode]---11.container with most water
description: Input: [1,8,6,2,5,4,8,3,7]Output: 49 思路1: 从(1,a1)开始向后算面积,需要两层n循环,时间复杂度n2 思路2: 找出数组中最大的数 ...
- 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode 11. Container With Most Water (装最多水的容器)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 11. Container With Most Water 装水最多的容器
[抄题]: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
随机推荐
- AI 领域与概述
概述 数据分析行业主要的职业发展. 业务:业务分析师.数据产品经理.产品总监 技术:算法师.架构师.研发经理.研发总监 美工:BI工程师 人工智能,是数据分析的子集.人工智能主要包括 语音识别 自然语 ...
- 【leetcode困难】968. 监控二叉树
968. 监控二叉树 瞎**分析评论区Rui大佬的答案,这题想直接递归return min还是有坑的,分计数和状态.有个状态转换的思想
- css伪元素::before与::after使用基础示例
1.指定文本前后添加内容 <div class="box">test</div> .box::before{ content: 'before'; marg ...
- docker安装宝塔面板
1.下载centos docker docker pull centos:7.2.1511 2.运行镜像设置端口 docker run -d -it -p 4001:8888 -p 4000:80 - ...
- Linux--Centos安装VNC
参考:https://linux.cn/article-5335-1.html 安装 vnc 服务器 yum install tigervnc-server -y 配置 vnc .service 更改 ...
- win10设置开机以及开机无密码验证
1.开机自启动 将程序的exe的快捷方式放入下列文件夹中 C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp 2.开机无登录验证 ...
- 201503-1 图像旋转 Java
思路: 观察输入和输出可以发现,第三列输出为第一行,第二列输出为第二行,第一列输出为第三行.循环即可 import java.util.Scanner; //得分80,本题最高需要输入100W次,因为 ...
- BitcoinCore JSONRPC Java使用,创建账号,获取余额,转账等等...
1.首先要安装好bitcoin core服务 上一篇有怎么安装 下面代码支持多钱包多地址动态调用,但让我没用使用多地址,根据自己的需要然后封装方法就好 2.引入jar JavaBitcoinRpcC ...
- MyBatis从入门到精通(第2章):MyBatis XML方式的基本用法【insert用法、update用法、delete用法】
2.4 insert 用法 2.4.1 简单的 insert方法 在接口 UserMapper.java 中添加如下方法. /** * 新增用户 * @param sysUser * @retur ...
- ZJNU 1542 - 三角形(续)--中高级
从小到大排序后 先固定一遍,另外两边递增查找 即固定 i,j=i+1,k=j+1 然后让k递增到 a[i]+a[j]<=a[k] 时 此时不能凑成一个三角形 答案增加 k-1-j 组 此时不需要 ...