LeetCode 11. Container With Most Water
2016-04-11 11:40
232 查看
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.
Classical two pointers problem
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.
Classical two pointers problem
#include <vector> #include <climits> #include <iostream> using namespace std; int containMostWater(vector<int>& height) { if(height.size() <= 1) return 0; int i = 0; int j = height.size() - 1; int maxWater = 0; while(i < j) { int minHeight = min(height[i], height[j]); maxWater = max(maxWater, minHeight * (j - i)); if(height[i] > height[j]) j--; else i++; } return maxWater; } int main(void) { vector<int> height{2, 3, 4, 3, 2}; int maxWater = containMostWater(height); cout << maxWater << endl; }
相关文章推荐
- LeetCode(43)-Contains Duplicate II
- LeetCode(43)-Contains Duplicate II
- LeetCode(43)-Contains Duplicate II
- 报错:failed to get the task for process XXX(解决方案)
- Domain generalization 简介
- BaiduMap---百度地图官方Demo之调用百度地图(介绍如何调启百度地图实现自身业务功能)
- Command /usr/bin/codesign failed with exit code 1
- jenkins排错---Waiting for next available executor
- 使用anddroid studio编译报错: Error:Execution failed for task ':mergeDebugResources'.
- 关于人工智能和人类未来的吐槽
- git:Please enter a commit message to explain why this merge is necessary
- 分类模型的性能评估——以SAS Logistic回归为例(3): Lift和Gain
- 解决Failed to parse SourceMap: http:xxx 问题
- 6.8.1 qmgr_active_drain函数:从active队列发信
- 我与小娜(50):聂卫平向人工智能致敬
- LeetCode 219. Contains Duplicate II
- hdu 1021 Fibonacci Again
- Z-Stack分析--main函数
- PERC5/6 RAID如何配置?整合
- aidl笔记