leetcode11
public class Solution
{
//public int MaxArea(int[] height)
//{
// var max = 0;
// for (int i = 0; i < height.Length; i++)
// {
// for (int j = 0; j < height.Length; j++)
// {
// if (i == j)
// {
// continue;
// }
// var min = Math.Min(height[i], height[j]);
// var dif = Math.Abs(i - j);
// var product = min * dif;
// max = Math.Max(max, product);
// }
// }
// return max;
//}
public int MaxArea(int[] height)
{
int max = int.MinValue;
for (int i = , j = height.Length - ; i < j;)
{
if (height[i] > height[j])
{
max = Math.Max(max, height[j] * (j - i));
j--;
}
else
{
max = Math.Max(max, height[i] * (j - i));
i++;
}
}
return max;
}
}
下面这个解决方案算是一种搞笑吧,夸张的6796ms,居然也能AC!
class Solution:
def findTopTwoNum(self,height):
top1 = -1
top1_index = -1 top2 = -1
top2_index = -1
for i in range(len(height)):
if top1 < height[i]:
top1 = height[i]
top1_index = i for i in range(len(height)):
if i != top1_index and top2 < height[i]:
top2 = height[i]
top2_index = i return top1_index,top2_index def maxArea(self, height: 'List[int]') -> 'int':
#在height中找最大的两个数字,以及这两个数字的坐标
left,right = self.findTopTwoNum(height) ma = min(left,right) * (right - left)
for i in range(left,-1,-1):
for j in range(right,len(height)):
area = min(height[i],height[j]) * (j-i)
if area > ma :
ma = area
return ma
leetcode11的更多相关文章
- [array] leetCode-11. Container With Most Water-Medium
leetCode-11. Container With Most Water-Medium descrition Given n non-negative integers a1, a2, ..., ...
- LeetCode11 Container With Most Water
题意: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- LeetCode-11. 盛最多水的容器
给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...
- [Swift]LeetCode11. 盛最多水的容器 | Container With Most Water
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- Leetcode11 Container With Most Water 解题思路 (Python)
今天开始第一天记录刷题,本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 准备按tag刷,第一个tag是array: 这个是array的第一道题:11. Container With ...
- LeetCode11:Container With Most Water
public int MaxArea(int[] height) { ; ; ; while(start<end) { max=Math.Max(max,Math.Min(height[star ...
- LeetCode11.盛最多水的容器
给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...
- 思维题(两点逼近)LeetCode11 Container with Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- LeetCode11.盛最多水的容器 JavaScript
给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...
随机推荐
- os、os.path模块(文件/目录方法)
1.模块的概念:模块是一个包含所有定义的变量.函数的文件,模块可以被其余模块调用. 2.利用OS模块实现对系统文件的. os模块中常见的方法: gercwd() 返回当前工作目录 chdir( ...
- Python2入门(1)
一.基础语法1 - 输出语句 print "hello world",print默认输出换行,如果需要实现不换行需在变量末尾加上逗号,; 2 - python合法标识符 3 - 字 ...
- Matlab_xcorr_互相关函数的讨论
假设两个平稳信号 $\textbf{x}$ 和 $\textbf{y}$ ,如果 $x\left(t+\tau\right)= y\left(t\right)$ ,则可通过互相关求 $\tau$ .由 ...
- cython 成功创建import 模块
又是因为别人代码里有这么一个部分,用到了cython,,简而言之,就是利用这个模块调用C语言,从而加速程序运行,其中具体怎么调用我还没整清楚,但基本用法差不多了解了. 1 安装:https://www ...
- Post Order traverse binary tree using non-recursive way
Process analysis Stack = 5, Push 3, Stack = 5, 3. Pre = 5 Current = 3, Pre = 5, Push 2 to the st ...
- Python全栈之路----函数----匿名函数
用lambda声明匿名函数,对lambda定义名字,才能被调用.下面的calc和func功能一致. def calc(x,y): return x*y func = lambda x,y: x*y # ...
- kali安装Google浏览器之后的问题
kali中,在安装完Google浏览器后会出现点击图标却打不开的问题,解决方式如下: 2019-04-10 09:46:00
- 关于java类加载的那些事
首先,类加载的定义: 类加载(Class Loading)是一种机制,他描述的是将字节码以文件形式加载到内存再经过连接.初始化后,最终形成可以被虚拟机直接使用的Java类型地过程. 其次,类加载器的分 ...
- robot framework下载文件操作
不同的浏览器点击[下载]按钮之后,需要点击保存,还是确定,或者直接默认直接下载都是不一样的 1.chrome:点击[下载]之后,会自动执行下载操作,直到下载结束 A)点击下载,等待下载结束(sleep ...
- [R] t.test()
t.test(x, y = NULL, alternative = c("two.sided", "less","greater"), mu ...