LeetCode11——Container With Most Water
来源:互联网 发布:2017淘宝618活动时间 编辑:程序博客网 时间:2024/05/16 23:45
常写算法,多动脑,不会老!
题目描述:
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.
题目我就不翻译啦!都是明日之星!!
解题思路:
这题如果暴力破解,估计凶多吉少,笔者没有试过,估计会报超时!思路很简洁,也很高效。采用从两头往中间靠的思路,面积计算公式为:
currentArea = (right - left)*min(height[left],height[right]);
如果能理解到这,那本题就算是搞定了!
贴一个非常简洁高效的 C++ 代码:
int maxArea(vector<int>& height) { int lengthHeight = (int)height.size(); int left = 0; int right = lengthHeight - 1; int currentArea = 0; int maxarea = 0; while (left < right) { currentArea = (right - left)*min(height[left],height[right]); maxarea = max(currentArea,maxarea); if (height[left] < height[right]) { left ++; } else { right --; } } return maxarea;}
代码效率:
阅读全文
1 0
- LeetCode11——Container With Most Water
- Leetcode11: Container with most water
- [LeetCode11]Container With Most Water
- LeetCode11:Container With Most Water
- leetcode11 Container With Most Water
- leetcode11 Container With Most Water
- LeetCode11:Container With Most Water
- Leetcode11 Container With Most Water
- leetcode11:Container With Most Water
- LeetCode11: Container With Most Water
- leetcode11. Container With Most Water
- leetcode11. Container With Most Water
- LeetCode11-Container With Most Water
- LeetCode11. Container With Most Water
- LeetCode11. Container With Most Water
- LeetCode11. Container With Most Water
- leetcode11. Container With Most Water
- LeetCode11. Container With Most Water
- 【深入浅出jQuery】源码浅析--奇技淫巧
- Tensorflow实战(一)(MNIST手写字体识别TensorFlow实现)
- BoundingBox工具及其使用方法
- 可编辑的DIV(兼容IE8)以及插入内容到光标位置
- 冒泡排序(C指针)
- LeetCode11——Container With Most Water
- 启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误的解决方法!
- AppCompatActivity 去标题栏
- 大家都这样优化简历
- TOMCAT-7.0.82,在部署项目后,JSP页面上引入的jar
- Object 类
- Hibernate 自定义查询sql 并使用自定义对象接收查询结果
- 递归---Hanoi塔问题
- LINUX下实现UDP传输文件