Container With Most Water(LintCode)

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.

Example

Given [1,3,2], the max area of the container is 2.

Note

You may not slant the container.

从数组两头遍历,计算面积,然后从小的那一段a开始找到下一个比a大的元素。直到所有数组都被遍历。

 1 public class Solution {
 2     /**
 3      * @param heights: an array of integers
 4      * @return: an integer
 5      */
 6     public int maxArea(int[] heights) {
 7         int l = heights.length;
 8         if (l == 0) {
 9             return 0;
10         }
11         int max = -1;
12         int i = 0;
13         int j = l-1;
14         while (i < j) {
15             int v = (j - i) * Math.min(heights[i],heights[j]);
16             if (max < v) {
17                 max = v;
18             }
19
20             if(heights[i] > heights[j]){
21                 int x = heights[j];
22                 while (heights[j] <= x && i < j) j--;
23             }else {
24                 int x = heights[i];
25                 while(heights[i] <= x && i < j) i++;
26             }
27         }
28         return max;
29     }
30 }

时间: 2024-07-31 12:03:03

Container With Most Water(LintCode)的相关文章

#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】Container With Most Water(middle)

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_11题——Container With Most Water(两个指针)

Container With Most Water Total Accepted: 38917 Total Submissions: 121822My Submissions Question Solution 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

在Azure Container Service创建Kubernetes(k8s)群集运行ASP.NET Core跨平台应用程序

引子 在此前的一篇文章中,我介绍了如何在本地docker环境中运行ASP.NET Core跨平台应用程序(http://www.cnblogs.com/chenxizhang/p/7148657.html),看起来非常不错,不是吗?那么,如果我们希望真正在实际的生产环境去部署和运行这个应用程序,应该怎么做呢? 通常来说,有两种方案可以选择 1. 在目标运行环境(可以是本地的服务器,也可以是云端)申请虚拟机,然后启用docker运行这些应用程序,所有的细节都可以(也必须)由你自己控制. 2. 使用

LeetCode 11 Container With Most Water(最大水容器)

翻译 给定n个非负整数a1,a2,...,an,其中每个代表一个点坐标(i,ai). n个垂直线段例如线段的两个端点在(i,ai)和(i,0). 找到两个线段,与x轴形成一个容器,使其包含最多的水. 备注:你不必倾倒容器. 翻译 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such tha

LeetCode 11. 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

判断数独是否合法(LintCode)

判断数独是否合法 请判定一个数独是否有效. 该数独可能只填充了部分数字,其中缺少的数字用. 表示. 样例 下列就是一个合法数独的样例. 注意 一个合法的数独(仅部分填充)并不一定是可解的.我们仅需使填充的空格有效即可. 说明 什么是 数独? http://sudoku.com.au/TheRules.aspx http://baike.baidu.com/subview/961/10842669.htm 一开始认为会超时于是有了用空间换时间的想法于是出现如下代码.. 后来一想,应该不会超时把..

木材加工(LintCode)

木材加工 有一些原木,现在想把这些木头切割成一些长度相同的小段木头,需要得到的小段的数目至少为 k.当然,我们希望得到的小段越长越好,你需要计算能够得到的小段木头的最大长度. 样例 有3根木头[232, 124, 456], k=7, 最大长度为114. 注意 木头长度的单位是厘米.原木的长度都是正整数,我们要求切割得到的小段木头的长度也要求是整数.无法切出要求至少 k 段的,则返回 0 即可. 挑战 O(n log Len), Len为 n 段原木中最大的长度 要达到n*log Len ,可以

二进制求和(LintCode)

二进制求和 给定两个二进制字符串,返回他们的和(用二进制表示). 样例 a = 11 b = 1 返回 100 细节出了好多问题,提交了好多次... 1 public class Solution { 2 /** 3 * @param a a number 4 * @param b a number 5 * @return the result 6 */ 7 public String addBinary(String a, String b) { 8 int c = 0; 9 int al =