Container With Most Water 解答
2015-09-17 03:46
369 查看
Question
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.
Solution
Since the area size is largely depended on shorter height, so we use two pointers. Time complexity O(n), space cost O(1).public class Solution { public int maxArea(int[] height) { if (height == null || height.length < 2) return 0; int left = 0, right = height.length - 1; int result = 0, tmp = 0; while (left < right) { tmp = (right - left) * Math.min(height[left], height[right]); result = Math.max(result, tmp); if (height[left] <= height[right]) left++; else right--; } return result; } }
相关文章推荐
- [CareerCup] 9.1 Climbing Staircase 爬楼梯
- cf#320 Div.2 Problem A Raising Bacteria
- Codeforces Round #320 (Div. 2) A. Raising Bacteria
- rails s的时代一去不复返了
- Email正则表达式日常整理大全
- exchange email account最多只能在10个终端上设置
- ZOJ.2060 Fibonacci Again【数论-斐波那契】 2015/09/16
- [BetterExplained]为什么你应该(从现在开始就)写博客
- 使用dir()、readdir()和glob()遍历目录及测试性能 http://blog.csdn.net/hpy1165331898/article/details/44918879
- RAID详解[RAID0/RAID1/RAID5/RAID10]
- mdadm使用详解及RAID 5简单分析
- 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATI
- Debain下源码安装OpenVAS
- 使用slidingmeu_actionbarsherlock_lib的问题和The hierarchy of the type MainActivity is inconsistent
- Async/Await - Best Practices in Asynchronous Programming
- [MFC]OnPaint双缓冲绘图
- 发邮件MailMessage、SmtpClient
- 关于错误Failed to open dex from file descriptor for zip file
- 运用ocropy时报numpy.dot() fails when using keyword argument "out"的错误
- xcode删除main.Storyboard 保持工程干净 需要注意事项