Container With Most Water
2016-07-14 08:50
423 查看
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate
Notice
You may not slant the container.
Example
Given
Analysis:
Begin with the first point and last point, calculate the container area. Move the first point forward or last point backward if it is smaller.
(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.
Notice
You may not slant the container.
Example
Given
[1,3,2], the max area of the container is
2.
Analysis:
Begin with the first point and last point, calculate the container area. Move the first point forward or last point backward if it is smaller.
public class Solution { /** * @param heights: an array of integers * @return: an integer */ public int maxArea(int[] height) { if (height == null || height.length <= 1) return 0; int max = 0; int i = 0; int j = height.length - 1; while (i < j) { if (max < Math.min(height[i], height[j]) * (j - i)) { max = Math.min(height[i], height[j]) * (j - i); } if (height[i] < height[j]) { i++; } else { j--; } } return max; } }
相关文章推荐
- BlockChain 与 Ethereum 介绍
- 上拉刷新下拉加载xlistview--------activity_main
- 软件工程技术
- ssh中“Host key verification failed.“的解决方案
- LeetCode 373. Find K Pairs with Smallest Sums
- You are using pip version 8.0.3, however version 8.1.2 is available. You should consider upgrading v
- UVA 253 Cube painting
- mysql解决fail to open file的方法
- 【一天一道LeetCode】#217. Contains Duplicate
- 【一天一道LeetCode】#217. Contains Duplicate
- 安装Genymotion + Virtualbox 出现 “不能为虚拟电脑打开一个新任务 E_FAIL(0x80004005)”错误的解决办法
- 217. Contains Duplicate
- Ex2010-18 Exclude email address/recipient from Exchange 2010 dynamic distribution group
- 论文阅读(1)--Fine-grained Image Classification by Exploring Bipartite-Graph Labels
- 人工智能常用 API
- 错误“Error adding system: Data not available.”警告“GI output for inputsystem: 8589dc02183b182ccba8c3”的解决
- jetBrains WebStrom的一种“破解”方式(不失一般性)
- airflow开启自动化schedule
- after command failed: Directory not empty
- API - PtOSContainer