LeetCode Container With Most Water
2015-06-01 15:12
405 查看
Description:
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.
Solution:
In this problem, we first assume that the whole length can get the maximum square. And then we try to reduce the scope by making the smaller height[i]/height[j] bigger.
e
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.
Solution:
In this problem, we first assume that the whole length can get the maximum square. And then we try to reduce the scope by making the smaller height[i]/height[j] bigger.
public class Solution { public int maxArea(int[] height) { int i = 0; int j = height.length - 1; int ans = 0; for (; i < j;) { ans = Math.max(ans, Math.min(height[i], height[j]) * (j - i)); if (height[i] < height[j]) i++; else j--; } return ans; } }
e
相关文章推荐
- http://blog.csdn.net/liuhe688/article/details/6715983/
- YT02-简单数学课堂题-1004 Fibonacci Again -(5.31日-烟台大学ACM预备队解题报告)
- Error Domain=NSCocoaErrorDomain Code=3000 "未找到应用程序的“aps-environment”的权利字符串" UserInfo=0x1bad30 {NSLocalizedDescription=未找到应用程序的“aps-environment”的权利字符串}
- Installation error: INSTALL_FAILED_CONFLICTING_PROVIDER 解决方案
- HackerRank - "Detecting Valid Latitude and Longitude Pairs"
- http error 503. the service is unavailable. sharepoint
- Leetcode Problem.172—Factorial Trailing Zeroes
- AIX下的nmon的使用
- 在可以调用OLE之前,必须将当前线程设置为单线程单元(STA)模式,请确保您的Main函数带有STAThreadAttribute
- shrio 权限管理filterChainDefinitions过滤器配置
- httpd: Could not reliably determine the server's fully qualified domain name
- leetcode_Contains Duplicate II_easy
- EAI和ESB
- leetcode_Contains Duplicate_easy
- AEAI CRM客户关系管理V1.0.3版发版说明
- 【译】使用Rails 4.2+ 测试异步邮件系统
- CodeForces 538E Demiurges Play Again(博弈dp)
- mail 命令指定smtp服务器发送邮件
- hdu 1022 Train Problem I (栈的应用)
- DELL服务器装2003系统