LeetCode 11. Container With Most Water
来源:互联网 发布:网络在线直播 编辑:程序博客网 时间:2024/06/07 11:21
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.
Subscribe to see which companies asked this question.
一个整数数组,序号代表底,数组值表达高度,找到一个能装最多水的情况。
能想到最好的办法是o(n)的,两个指针一个从数组头向尾部走,另一个从尾部往头部走。没走一步做两个对比:
1.当前的面积和max比是否更大,更大则替换max值;
2.比较两个指针谁更小,小的向中间走一步;原因是小的不会得到更高的值(它的高度就是限制)
public class Solution { public int maxArea(int[] height) { int max = 0; int len = height.length; int i = 0,j = len - 1; while(i!=j){ int a = (j-i)*Math.min(height[i],height[j]); max = a>max?a:max; if(height[i]>height[j])j--; else i++; } return max; }}
0 0
- LeetCode 11. Container With Most Water
- [LeetCode]11.Container With Most Water
- LeetCode --- 11. Container With Most Water
- LeetCode 11.Container With Most Water
- [Leetcode] 11. Container With Most Water
- [leetcode] 11.Container With Most Water
- 【leetcode】11. Container With Most Water
- [LeetCode]11. Container With Most Water
- 11. Container With Most Water LeetCode
- leetcode 11. Container With Most Water
- leetcode 11.Container With Most Water
- [LeetCode OJ]11. Container With Most Water
- [leetcode] 11. Container With Most Water
- Leetcode 11. Container With Most Water
- leetcode 11.Container With Most Water
- leetcode 11. Container With Most Water
- LeetCode 11. Container With Most Water
- LeetCode *** 11. Container With Most Water
- S5PV210系列(裸机一) S5PV210的启动过程详解
- FASTDFS 文件同步
- JAVA获取当前路径的几种方法
- 组件Navigator的使用
- 微信小程序滚动到某个位置改变效果
- LeetCode 11. Container With Most Water
- DSP实验四(DSP的定时器)
- 简单粗暴地理解 JS 原型链
- ARMv8-AArch64简述
- 数论——bzoj3560: DZY Loves Math V
- Android应用开发———Android简介
- JavaScript中的对象(1)
- eclipse中格式化代码快捷键Ctrl+Shift+F失效
- 模块计算机类型x64与目标计算机类型X86冲突