[LeetCode]-011-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.
题目大意:求一系列点之间的最大面积
第一种方法:时间复杂度(O(n^2))
public int maxArea(int[] height) {
int area = 0;
int min = 0;
for(int i=0; i<height.length; i++){
for(int j=i+1; j<height.length; j++){
if(height[i] == height[j])
area = Math.max(area, height[i]*(j-i));
else{
min = Math.min(height[i],height[j]);
area = Math.max(area, min*(j-i));
}
}
}
return area;
}
第二种方法:
public int maxArea(int[] height) {
int area = 0;
int begin = 0;
int end = height.length-1;
while(begin<end){
area = Math.max(area, (end-begin)*Math.min(height[begin],height[end]));
if(height[begin]<height[end])
begin++;
else
end--;
}
return area;
}
[LeetCode]-011-Container_With_Most_Water的更多相关文章
- 【JAVA、C++】LeetCode 011 Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [Leetcode]011. Container With Most Water
public class Solution { public int maxArea(int[] height) { int left = 0, right = height.length - 1; ...
- leetcode python 011
####给定n个非负整数a1,a2,...,an,其中每个表示坐标(i,ai)处的点.##绘制n条垂直线,使得线i的两个端点位于(i,ai)和(i,0).##找到两条线,它们与x轴一起形成一个容器,这 ...
- 【LeetCode】011 Container With Most Water
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- [LeetCode] Binary Watch 二进制表
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom ...
- [LeetCode] Bitwise AND of Numbers Range 数字范围位相与
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- [LeetCode] Restore IP Addresses 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- [LeetCode] Stickers to Spell Word 贴片拼单词
We are given N different types of stickers. Each sticker has a lowercase English word on it. You wou ...
- [LeetCode] Bulb Switcher II 灯泡开关之二
There is a room with n lights which are turned on initially and 4 buttons on the wall. After perform ...
随机推荐
- CSP-S 2019游记
Day 0 下午到了广州,酒店还不错,不好的是附近没有什么吃饭的地方 zyd和ljz巨神说如果上了450就女装. 晚上看了一下写过模板,本来准备敲几个新模板的的结果被卡常,心态没了.于是又把wys的卡 ...
- [Vue] vue的一些面试题2
1.Vue.observable 你有了解过吗?说说看 vue2.6 发布一个新的 API,让一个对象可响应.Vue 内部会用它来处理 data 函数返回的对象.返回的对象可以直接用于渲染函数和计算属 ...
- 剑指offer-2:斐波那契数列
二.斐波那契数列 题目描述 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0). n<=39 1.递归法 1). 分析 斐波那契数列的标准公式为 ...
- spring boot基础学习教程
Spring boot 标签(空格分隔): springboot HelloWorld 什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新 ...
- [转载]一个支持Verilog的Vim插件——自动插入always块
原文地址:一个支持Verilog的Vim插件--自动插入always块作者:hover 插件支持always块的自动插入,如果用户要插入时序always块,需要在端口声明中标志时钟和异步复位信号(仅支 ...
- 2>&1的含义解释
某次想将adb的help信息保存起来 F:\android系统\AndroidTool_Release_v2.\bin>adb >help.txt 结果 F:\android系统\Andr ...
- Idea集成使用SVN教程
第一步:下载svn的客户端,通俗一点来说就是小乌龟啦!官网下载地址:https://tortoisesvn.net/downloads.html 下载之后直接安装就好了,但是要注意这里,选择安装所有的 ...
- pyinstaller 打包工具的使用方法
pyinstaller的安装 下载后可以输入pip list查看是否安装成功 然后切换到项目的根目录输入 pyinstaller -i favicon.ico -w -c game.py -p Que ...
- Java并发编程实战 第5章 构建基础模块
同步容器类 Vector和HashTable和Collections.synchronizedXXX 都是使用监视器模式实现的. 暂且不考虑性能问题,使用同步容器类要注意: 只能保证单个操作的同步. ...
- Canvas + WebSocket + Redis 实现一个视频弹幕
原文出自:https://www.pandashen.com 页面布局 首先,我们需要实现页面布局,在根目录创建 index.html 布局中我们需要有一个 video 多媒体标签引入我们的本地视频, ...