LeetCode: Container With Most Water 解题报告

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.

SOLUTION 1:

采用2个指针一个在左一个在右,计算『桶』可以容纳的水。如果左边低,则左指针右移(试图看可不可以找到更高的bar),反之左移右指针

 1 public class Solution {
 2     public int maxArea(int[] height) {
 3         if (height == null) {
 4             return 0;
 5         }
 6
 7         int left = 0;
 8         int right = height.length - 1;
 9         int maxArea = 0;
10
11         while (left < right) {
12             int h = Math.min(height[left], height[right]);
13             int area = h * (right - left);
14             maxArea = Math.max(maxArea, area);
15
16             if (height[left] < height[right]) {
17                 // 如果左边界比较低,尝试向右寻找更高的边界
18                 left++;
19             } else {
20                 // 如果右边界比较低,尝试向左寻找更高的边界
21                 right--;
22             }
23         }
24
25         return maxArea;
26     }
27 }

代码:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/twoPoints/MaxArea.java

时间: 2024-10-10 15:51:38

LeetCode: Container With Most Water 解题报告的相关文章

【LeetCode】11. Container With Most Water 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51820937 Subject 出处:https://leetcode.com/problems/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

【LeetCode】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 con

【LeetCode】Trapping Rain Water解题报告

[题目] Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. [解析] 题意:求数组组成的凹槽能盛水的容积. 思路:从两端向中间靠拢,求以

LeetCode: Pascal&#39;s Triangle II 解题报告

Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question SolutionGiven an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to us

LeetCode: 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 containe

LeetCode &quot;Container With Most Water&quot; - GREEDY?

O(n^2) is a naive solution. As rule of thumb, there must be O(n) soluion. Yes - Greedy. We assume widest container contains the most water, greedily, but it is possible that we have higher heights with narrower range, so we proceed. Of course we disg

#LeetCode# Container With Most Water (todo)

描述: 实现1 -- 求所有可能的值,O(N^2),超时了(因为超时没有跑所有的测试用例,所以不确定还有没有其他问题) 代码: 1 def maxArea(self, height): 2 tmp = len(height) 3 if tmp == 0 or tmp == 1: return 0 4 if tmp == 2: return abs(height[1] - height[0]) 5 6 minus_lst = [height[i] - height[i-1] for i in ra

【LeetCode】Word Search II 解题报告

[题目] Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The sa

【LeetCode】Course Schedule II 解题报告

[题目] There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of courses an