leetcode 011 —— Container With Most Water

原创 2015年07月08日 19:39:44

Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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.

题意:数组[a1a2, ..., an]代表板子的高度,求两块板子,使得所能盛水的量最多;

思路:贪心算法

两条边往中间移动,搜索最大装水量。每次都是较短的边往中间移。

class Solution {
public:
    int maxArea(vector<int>& height) {
    	int left = 0;
    	int right = height.size()-1;
    	int max = 0;
    	while (left < right){
    		const int size = (right - left)min(height[left],height[right] );
    		max = size > max ? size : max;
    
    		if (height[left] > height[right])
    			right--;
    		else
    			left++;
    	}
    	return max;
    }
};





LeetCode011 Container With Most Water

详细见:leetcode.com/problems/container-with-most-water/ Java Solution: github package leetcode;...
  • zxwtry
  • zxwtry
  • 2017年03月28日 17:21
  • 101

leetcode之路011 Container With Most Water

题目大意:给定n个非负的整数a1,a2...ai,每个点表示一个坐标(i,ai),从坐标轴上画n条直线,分别是连接(i,0)和(i,ai)的。其中任意两条直线看成一个容器,向里面装水,现在要找到所有容...

Leetcode011. Container With Most Water

题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i,...

LeetCode 011 Container With Most Water

【题目】 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i...

leetcode——Container With Most Water

题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinat...

leetcode第11题——**Container With Most Water

题目 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i,...
  • buptlrw
  • buptlrw
  • 2016年01月06日 17:08
  • 349

LeetCode刷题(C++)——Container With Most Water

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai)....

《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动

问题Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). ...

Leetcode Q11:Container With Most Water

题目11: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (...

LeetCode_Container With Most Water

Container With Most Water   Given n non-negative integers a1, a2, ..., an, where each repr...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode 011 —— Container With Most Water
举报原因:
原因补充:

(最多只允许输入30个字)