11. Container With Most Water (JAVA)
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.
Example:
Input: [1,8,6,2,5,4,8,3,7]
Output: 49
class Solution {
public int maxArea(int[] height) {
return calMax(height, 0, height.length-1, 0);
}
public int calMax(int[] height, int left, int right, int max){
if(left >= right) return max;
if(height[left] > height[right]){
int area = (right-left) * height[right];
return calMax(height, left, right-1, Math.max(area,max));
}
else{
int area = (right-left) * height[left];
return calMax(height, left+1, right, Math.max(area,max));
}
}
}
解题思路:两个指针分别指向左右两个位置,保留高的那个,矮的那个往中间移动。(Greedy:总是寻找高的柱子)
11. Container With Most Water (JAVA)的更多相关文章
- 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 ...
- [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: 找出数组中最大的数 ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- Java [leetcode 11] Container With Most Water
问题描述: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
- LeetCode 11. Container With Most Water (装最多水的容器)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
随机推荐
- 服务器-华为RH5885 V3-安装Windows Server 2008R2后设备管理器中存在大量的感叹号,并且无法识别网络适配器,没有网卡
问题描述:用引导盘安装Windows Server 2008R2后,出现如题的情况. 根源:驱动未安装. 解决方法: 1.下载驱动:https://support.huawei.com/enterpr ...
- wikipedia 维基百科 语料 获取 与 提取 处理 by python3.5
英文维基百科 https://dumps.wikimedia.org/enwiki/ 中文维基百科 https://dumps.wikimedia.org/zhwiki/ 全部语言的列表 https: ...
- 冒号课堂 编程范式与OOP思想
上篇:编程范式与编程语言 第1课 开班导言 第2课 重要范式 第3课 常用范式 第4课 重温范式 第5课 语言小谈 第6课 语言简评 下篇:抽象机制与对象范式 第7课 抽象封装 第8课 抽象接口 第9 ...
- 我发起并创立了一个 C 语言编译器 开源项目 InnerC
本文是 VMBC / D# 项目 的 系列文章, 有关 VMBC / D# , 见 <我发起并创立了一个 VMBC 的 子项目 D#>(以下简称 <D#>) https: ...
- Java8 新特性学习
摘自:https://blog.csdn.net/shuaicihai/article/details/72615495 Lambda 表达式 Lambda 是一个匿名函数,我们可以把 Lambda ...
- QQ群成员发言次数统计(正则表达式版)
1.先将QQ群的消息记录以.txt文件格式导出来,保存路径及名称自己定义(在本文我导出到Y盘,命名为test.txt) 2.程序如下: data statistics1; if _n_=1 then ...
- bvlc_reference_caffenet网络权值可视化
一.网络结构 models/bvlc_reference_caffenet/deploy.prototxt 二.显示conv1的网络权值 clear; clc; close all; addpath( ...
- powershell中设置变量并启动Tomcat
假设tomcat安装在 C:\GreenSoftware\apache-tomcat-9.0.14 目录. 使用powershell进入到此目录.执行命令 $Env:JAVA_HOME="C ...
- 一些有用的Java学习资料
Better Java,一些好的Java实践 Google Java Style Guide 30个Java编程技巧 JDK8新增语法特性简介,对Java8中新增的函数接口.Lambda表达式.方法引 ...
- 京东返利渠道,自己拿返利,无需A推B操作
京东返利渠道,自己拿返利,无需A推B操作,简单快捷方便 1.在微信小程序中搜索 “京东饭粒” 2.进入京东饭粒,进购物车下单(只能在购物车内下单返利) 3.收货后26天返京豆到你的京东账号中,”佛系返 ...