蜗牛慢慢爬 LeetCode 11. Container With Most Water [Difficulty: Medium]
题目
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 and n is at least 2.
翻译
给定n个非负整数a1,a2,...,an,其中每个表示坐标(i,ai)处的点。绘制n条垂直线,使得线i的两个端点处于(i,ai)和(i,0)。找到两条线,它们与x轴一起形成容器,使得容器含有最多的水。
Hints
Related Topics: Array, Two Points
容器是有木桶效应的 也就是说短的垂直线决定了整个容器的高 所要求的是 max(短边*坐标差)
找到一些最大值的一般想法是通过可能发生最大值的所有情况,并继续更新最大值。为了提高扫描效率,要找到一种智能的扫描方式来切断无用的情况,同时100%保证通过其他情况可以达到最大值
这儿可以通过设置左右两端的初始指针,向中间扫描,每次只移动较小值的指针 每一次移动都更新最大值
代码
Java
class Solution {
public int maxArea(int[] height) {
int left,right;
left = 0;right = height.length-1;
int maxArea = 0;
while(left<right){
maxArea = Math.max(maxArea, (right-left)*(Math.min(height[left],height[right])));
if(height[left]<height[right])
left++;
else
right--;
}
return maxArea;
}
}
Python
class Solution(object):
def maxArea(self, height):
l = 0; r = len(height)-1;
maxArea = 0
while l<r:
maxArea = max(maxArea, (r-l)*min(height[l],height[r]))
if height[l]<height[r]:
l += 1
else:
r -= 1
return maxArea
蜗牛慢慢爬 LeetCode 11. Container With Most Water [Difficulty: Medium]的更多相关文章
- 蜗牛慢慢爬 LeetCode 8. String to Integer (atoi) [Difficulty: Medium]
题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cas ...
- 蜗牛慢慢爬 LeetCode 23. Merge k Sorted Lists [Difficulty: Hard]
题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- 蜗牛慢慢爬 LeetCode 25. Reverse Nodes in k-Group [Difficulty: Hard]
题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...
- # 蜗牛慢慢爬 LeetCode 21. Merge Two Sorted Lists [Difficulty: Easy]
题目 Merge two sorted linked lists and return it as a new list. The new list should be made by splicin ...
- leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II
11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...
- Leetcode 11. Container With Most Water(逼近法)
11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]
题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]
题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
随机推荐
- 20155226 2016-2017-2 《Java程序设计》第9周学习总结
20155226 2016-2017-2 <Java程序设计>第9周学习总结 教材学习内容总结 JDBC简介 JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数据库厂商 ...
- JavaScript总结(三)
如何执行代码语句? 使用函数,函数是一组可以随时随地运行的语句,它们是JavaScript的核心.函数是由关键字function.函数名加一组参数以及置于括号中要执行的代码声明的.语法如下: Func ...
- 学习笔记:启动对特定用户的会话的sql跟踪
学习 Oracle性能诊断艺术 制作一个role: CREATE ROLE sql_trace; 然后再作一个数据库登录级别的触发器: CREATE OR REPLACE TRIGGER enable ...
- PostgreSQL timeline
磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL基础知识与基本操作索引页 回到顶级页面:PostgreSQL索引页 关于timeline,有如下的说法 http ...
- python基础学习1-类,对象
class Foo:#定义类 def mail(self,email,message):#定义类的方法 print('发送邮件给%s! 信息:%s'% (email,message)) return ...
- AGC 005 D - ~K Perm Counting
D - ~K Perm Counting 链接 题意: 求有多少排列对于每个位置i都满足$|ai−i|!=k$.n<=2000 分析: 容斥+dp. $answer = \sum\limits_ ...
- AtCoder ExaWizards 2019 D Modulo Operations
题意 给出一个长度为\(n\)的数列和数字\(X\),对于数列的每一种排列,其权值\(X\)依次对排列中的数取模,求出\(n!\)种情况最后剩下的数的权值和 分析 如果大的数字排在小的数字后面,那么大 ...
- 【Maven】在pom.xml文件中使用resources插件的小作用
在spring boot创建web项目打包为jar包的过程中,是不会把webapp目录下的页面也打包进去的,这个时候接触到了maven的 resources插件. ================== ...
- request不能接受前端传来的参数的问题
这是因为设置了 disabled ,所以接收不到参数,只需要去掉disabled即可.
- 【Java多线程】Executor框架的详解
在Java中,使用线程来异步执行任务.Java线程的创建与销毁需要一定的开销,如果我们为每一个任务创建一个新线程来执行,这些线程的创建与销毁将消耗大量的计算资源.同时,为每一个任务创建一个新线程来执行 ...