LeetCode 11. Container With Most Water–Java 解法

此文首发于我的个人博客:LeetCode 11. Container With Most Water–Java 解法–困雨水简单版 — zhang0peter的个人博客


LeetCode题解文章分类:LeetCode题解
LeetCode 所有题目总结:LeetCode 所有题目总结


题目地址:Container With Most Water - LeetCode


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.

Example:

Input: [1,8,6,2,5,4,8,3,7]
Output: 49

很奇怪这道题目我这么晚才看到,之前看到LeetCode 42. Trapping Rain Water时觉得题目怎么这么难,现在才发现,原来这道题目是简化版的困雨水。

这道题目难度的降低在于柱子是没有体积的,

暴力破解的自然会超时,如果从2遍向中间夹即可遍历一遍。

Java解法如下:

class Solution {public int maxArea(int[] height) {int maxarea = 0, left = 0, right = height.length - 1;while (left < right) {maxarea = Math.max(maxarea, Math.min(height[left], height[right]) * (right - left));if (height[left] < height[right]) {left++;} else {right--;}}return maxarea;}
}

做完这道题目后可以做一下升级难度的题:LeetCode 42. Trapping Rain Water–算法题–c++解法–困雨水复杂版

LeetCode 11. Container With Most Water--Java 解法--困雨水简单版相关推荐

  1. 【贪心+双指针】LeetCode 11. Container With Most Water

    LeetCode 11. Container With Most Water Solution1: 参考网址:http://www.cnblogs.com/grandyang/p/4455109.ht ...

  2. LeetCode 11 Container With Most Water

    问题:给出一个数组nums,要求选择2个,使得容器中包含的水最多. 思路:假设选取x,y,其中x<y,则包含的水为min(x,y)*(index(y) - index(x)).此时应该选择在最小 ...

  3. LeetCode 11.Container With Most Water

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  4. leetcode 11. Container With Most Water

    题意:题目大概意思就是让你找两个数使得两个数的最小值乘以他们的距离最大Area 题解:设置两个变量, 一个是从头开始,一个是从尾开始,计算两个数的Area,并更新最大值,如果开始的元素大于结尾的元素则 ...

  5. LeetCode 804 Unique Morse Code Words--python,java解法

    题目地址:Unique Morse Code Words - LeetCode Difficulty:easy Acceptance:74.1% International Morse Code de ...

  6. 【LeetCode】11. Container With Most Water 解题小结

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  7. leetcode -eleven:Container With Most Water

    2019独角兽企业重金招聘Python工程师标准>>> Given n non-negative integers a1, a2, ..., an, where each repre ...

  8. 11. Container With Most Water

    2019独角兽企业重金招聘Python工程师标准>>> 就是说,x轴上在1,2,...,n点上有许多垂直的线段,长度依次是a1, a2, ..., an.找出两条线段,使他们和x抽围 ...

  9. 基于java SSM图书管理系统简单版设计和实现

    本项目演示链接地址 > 主要技术:后台采用技术: SSM框架(SpringMVC + Spring + Mybatis) 前台采用技术: div + css 功能比较简单.适合学习使用 主要功能 ...

最新文章

  1. 自动化运维之CentOS7下PXE+Kickstart+DHCP+TFTP+HTTP无人值守安装系统
  2. wpf控件开发基础(1)
  3. Python爬虫入门并不难,就看你如何选择?
  4. linux查看apache端口,linux系统下Apache服务启动时80端口报错
  5. 深度学习-LeCun、Bengio和Hinton的联合综述-Deep Learning一篇nature综述论文
  6. nicetool好工具_N个办公辅助好工具,无需下载,简单实用
  7. thymeleaf(th:each th:selected) 从后台动态获取下拉框数据回显及选中
  8. PVS-Studio C/C++/C++11 静态代码分析工具
  9. erlang四大behaviour简述
  10. oracle進程時高時低,oracle低權限下獲取shell
  11. windbg分析Kernel32.dll导出表
  12. 想提高计算机ppt能力,实习了才发现,学好PPT有多重要!
  13. 【友盟+】助力故宫博物院数据化智能运营
  14. python 人脸识别活体检测_人脸识别 -- 活体检测(张嘴摇头识别)
  15. 计算机科学型,函数计算器1005B学生专用 多功能计算机科学函数型
  16. 在线音乐播放地址//歌曲URL地址提取
  17. 基于RT1052 Aworks 测试PXP图像混合功能(十三)
  18. PC端工具 WP7 PC端截图工具WP7 Screen recorder 发布 附使用教程
  19. python神经网络构建图_如何用卷积神经网络构建图像?
  20. Linux网络编程 - 域名(DNS)及网络地址

热门文章

  1. RDKit | 基于RDKit绘制化学反应
  2. GROMACS运行参数之npt.mdp文件详解
  3. python绘制动态图表怎么存下来_用python如何实现导入excel数据后自动生成图表?python如何实现交互式动态图表?...
  4. Perl 模块安装总结
  5. IJMS:牧医所奶业创新团队在瘤胃微生物新型脲酶抑制剂开发方面取得新进展
  6. NAR:antiSMASH数据库2—次级代谢物基因簇预测
  7. python使用openCV图像加载(转化为灰度图像)、Canny边缘检测器检测图像的边缘(Detect Edges)
  8. seaborn可视化水平箱图(Horizontal Boxplot in Python with Seaborn)
  9. Python使用matplotlib可视化绘制并通过Tkinter生成按钮将可视化结果导出为pdf文件
  10. R语言获取当前R版本(version)实战