Container With Most Water
2015-08-06 21:21
513 查看
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:
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:
class Solution { public: int maxArea(vector<int>& height) { int len = height.size(); int maxarea = 0; int i = 0, j = len - 1; while(i < j) { if(height[i] <= height[j]) { maxarea = max(maxarea, height[i] * (j - i)); i++; } else { maxarea = max(maxarea, height[j] * (j - i)); j--; } } return maxarea; } };
相关文章推荐
- 2015 HUAS Summer Trainning #4~D
- Doing Homework again SDUT 2076
- 2015 Multi-University Training Contest 2
- DELL R710 服务器更换硬盘后,重建RAID1 重装操作系统 无法启动
- POJ 1804 Brainman
- HDOJ1789Doing Homework again(贪心)
- 2015 Multi-University Training Contest 6 hdu 5360 Hiking
- HDU-- 2015 Multi-University Training Contest 6 Cake
- hdu 5360 Hiking(2015 Multi-University Training Contest 6)
- hdu 4750——Count The Pairs
- HDOJ 题目4251The Famous ICPC Team Again(划分树)
- hdu 5353 Average(2015 Multi-University Training Contest 6)
- HDU 5360 Hiking(优先队列)2015 Multi-University Training Contest 6
- POJ 3414 Paid Roads(状态压缩最短路)
- chain
- Codeforces Gym 100342C Problem C. Painting Cottages 暴力
- Codeforces A. Lineland Mail
- 2015 Multi-University Training Contest 6
- 实参和形参指针做函数参数时,如何改变main函数变量的值
- Keychain的简单使用