[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 ...
随机推荐
- 26. Remove Duplicates from Sorted Array(代码思路新奇)
Given a sorted array, remove the duplicates in-place such that each element appear only once and ret ...
- ubuntu 安装mysql5.7
一.Windows mysql5.6 解压版 安装 关于widnows平台上的安装教程,可参考百度经验: 链接:https://jingyan.baidu.com/article/f3ad7d0ffc ...
- Mysql数据库中的输入命令各类知识总结
1.链接数据库的命令---mysql-u root-p 回车,输入密码//在cmd上输入自己的账号密码 2.查看:show databases: 3.创建数据库:create +database+数据 ...
- js,el表达式,<c:if>
<c:if>中只能有一个判断语句,但是可以在一个el表达式中写多个判断条件 例: <c:if test="${fn:length(item.work_detail.note ...
- webpack常用的插件
webpack常用的开发插件 1.clean-webpack-plugin 运行webpack build时先把打包进入的文件夹清空 注意,它是以对象的方式去接收的 const { CleanWebp ...
- kbd_mode - 显示或者设置键盘模式
总览 (SYNOPSIS) kbd_mode [ -auks ] 描述 (DESCRIPTION) 如果 没有 参数 kbd_mode 会 显示 当前 键盘 的 模式, 如果 有 参数, 它会把 键盘 ...
- VMware® Workstation 设置虚拟机目录和共享目录不要相同!
在设置VMware的首选项是,工作区中的虚拟机的默认地址和共享虚拟机的位置目录不要设置成一样的. 否则创建的虚拟机打不开.
- Ansible批量部署工具
Ansible:自动化运维工具 你需要在一台机器上yum install 一个包,这时候有一个需求,比如现在有5台机同时需要装apache这个包,那么100台呢,ssh上去就太慢了,这时候就借助到了a ...
- jenkins打包maven工程发现有些包下载不下来
将这些依赖的jar包放到mvn的本地仓库中,通常是用户主目录下的.m2/repository https://blog.csdn.net/taiyangdao/article/details/5228 ...
- 对请求的request添加一些参数
- (NSURLRequest *)addHeaderRequestWithUrl:(NSString *)urlStr{ NSMutableURLRequest *mutableRequest ...