Container With Most Water leetcode 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.
题解:
这道题挺类似二分查找法的,这道题是先从两头开始算面积,面积的计算要由短板决定,并维护一个当前最大面积。
然后逐步替换小的短板来计算面积。每一步只替换短板的原因是,短板决定面积,而高板不影响,所以要想有所改变就改变那个有决定性的东西。。
网上有好多人都画出图来了。。可以搜搜看。。。
代码如下:
- 1 public int maxArea(int[] height) {
- 2 if(height == null || height.length == 0)
- 3 return 0;
- 4
- 5 int low = 0, high = height.length -1 ;
- 6 int max = 0;
- 7 while (low < high) {
- 8 int area = (high-low)*Math.min(height[low], height[high]);
- 9
- max = Math.max(max, area);
- if (height[low] < height[high])
- low++;
- else
- high--;
- }
- return max;
- }
Container With Most Water leetcode java的更多相关文章
- Container With Most Water - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Container With Most Water - LeetCode 注意点 没什么好注意的... 解法 解法一:暴力求解,假设任意两个端点会是最佳答 ...
- Container With Most Water -- LeetCode 11
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- Container With Most Water——LeetCode
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- Trapping Rain Water leetcode java
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...
- LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number
1. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- leetcode面试准备:Container With Most Water
leetcode面试准备:Container With Most Water 1 题目 Given n non-negative integers a1, a2, ..., an, where eac ...
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
随机推荐
- [leetcode tree]107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- 1022 Digital Library (30)(30 point(s))
problem A Digital Library contains millions of books, stored according to their titles, authors, key ...
- http常见请求头与响应头
1.HTTP常见的请求头 If-Modified-Since:把浏览器端缓存页面的最后修改时间发送到服务器去,服务器会把这个时间与服务器上实际文件的最后修改时间进行对比.如果时间一致,那么返回304, ...
- luoguP3979 遥远的国度 树链剖分
\(1, 2\)操作没什么好说的 对于\(3\)操作,分三种情况讨论下 \(id = rt\)的情况下,查整棵树的最小值即可 如果\(rt\)在\(1\)号点为根的情况下不在\(id\)的子树中,那么 ...
- KMP 解决串的模式匹配问题
初学KMP的时候,一直不得要领.后来学习AC自动机的时候,一下子明白了KMP实际上是AC自动机的特殊情况. 首先贴三段代码,一组是回溯法,暴力求解,另外两个是KMP串模式匹配 /* 回溯法字符串匹配算 ...
- bzoj 4092 DP
简化题意: 给定两个集合A,B,A集合有一个权值,并且对应一个B集合的子集,求A的一个子集,满足权值和最小且对应的子集的并集是B集合. 感觉像网络流,但因为每个B中的元素对应一个A中的元素就行了,是o ...
- UVALive 5059 C - Playing With Stones 博弈论Sg函数
C - Playing With Stones Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu S ...
- C#高级编程9-第1章.NET体系结构
C#与NET的关系 C#编写的所有代码必须使用.NET FrameWork运行 C#是一种语言,但它本身不是.NET的一部分 C#一些特性,.NET不支持,.NET一些特性,C#不支持 公共语言运行库 ...
- SpringBoot无法启动,Process finished with exit code 0
1.排查yml和properties文件是否配置错误 2.排查POM引入的包
- Django1.7如何配置静态资源访问
Django是非常轻量级的Web框架,今天散仙来看下如何在Django中配置静态的资源访问路径,一个中等规模的网站,可能就会有很多静态的资源需要访问,无论是html,txt,还是压缩包,有时候访问这些 ...