蜗牛慢慢爬 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 ...
随机推荐
- 20155327Exp2 后门原理与实践
20155327Exp2 后门原理与实践 一.实验说明 任务一:使用netcat获取主机操作Shell,cron启动 (0.5分) 任务二:使用socat获取主机操作Shell, 任务计划启动 (0. ...
- c语言数组的赋值问题
int arr[5]; 当此语句出现再main()之前时,所有的内容被自动赋值为0. 当此语句出现再main()之中时,所有的内容都保持原有内容不变. int arr[5]={0}; 当出现词类语句时 ...
- PostgreSQL PITR实验
磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL基础知识与基本操作索引页 回到顶级页面:PostgreSQL索引页 看PostgreSQL中与PITR相关的设定 ...
- jQuery学习-鼠标事件
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- loj#121.「离线可过」动态图连通性
题面 话说#122怎么做啊 题解 我的\(\mathrm{LCT}\)水平极差,连最小生成树都快忘了,赶紧复习一下 做法和这篇是一样的 这道题还可以练习线段树分治 还可以练习ETT 果然是道吼题 代码 ...
- 【Vijos】lxhgww的奇思妙想
题面 题解 求$k$级祖先孙子 为什么要用长链剖分啊??? 倍增并没有慢多少... 其实是我不会 长链剖分做这道题还是看这位巨佬的吧. 代码 #include<bits/stdc++.h> ...
- P3702 [SDOI2017]序列计数
P3702 [SDOI2017]序列计数 链接 分析: 首先可以容斥掉,用总的减去一个质数也没有的. 然后可以dp了,f[i][j]表示到第i个数,和在模p下是j的方案数,矩阵快速幂即可. 另一种方法 ...
- 使用终端命令行将本地项目上传到Github
使用终端命令行将本地项目上传到Github 转自https://blog.csdn.net/fishball1/article/details/52020305 对于IOS开发者来说,Github的使 ...
- Print Spooler 服务自动停止
1)先在服务里停止并禁用Print Spooler : 2)删除此文件夹下的所有文件,C:\Windows\System32\spool\PRINTERS\ : 3)删除注册表 HKEY_LOCAL_ ...
- PHP 用户密码加密函数password_hash
传统的用户名和密码都采用加盐的方式存储加密信息,盐值也需要存储. 自PHP5.5.0之后,新增加了密码散列算法函数(password_hash),password_hash() 使用足够强度的单向散列 ...