leetcode 11. Container With Most Water
来源:互联网 发布:记事本写c语言 编辑:程序博客网 时间:2024/05/17 22:00
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.
class Solution {public:int maxArea(vector<int>& height) {int heightSize = height.size();if (height.empty())return 0;if (heightSize < 2)return 0;int index=0;for (int i = 0; i < height.size(); i++)if (height[i]>height[index])index= i;if (height[index] == 0)return 0;int max = 0;vector<int>starter(heightSize);if (index < height.size() / 2){int nxt = 0; int i = 0;bool f = false;while (!f&&i < heightSize - 1){if (height[i] == 0)i++;else{f = true;i = nxt;nxt = heightSize;if (starter[i] == 0){for (int j = i + 1; j < heightSize; j++){if (height[j] <= height[i])starter[j] = 1;else{if (f){nxt = j;f = false;}}int h = height[i] < height[j] ? height[i] : height[j];if (h*(j - i)>max)max = h*(j - i);}}}}}else{int nxt = heightSize - 1; int i = heightSize - 1;bool f = false;while (!f&&i >= 1){if (height[i] == 0)i--;else{f = true;i = nxt;nxt = 0;if (starter[i] == 0 && height[i] > 0){for (int j = i - 1; j >= 0; j--){if (height[j] <= height[i])starter[j] = 1;else{if (f){nxt = j;f = false;}}int h = height[i] < height[j] ? height[i] : height[j];if (h*(i - j)>max)max = h*(i - j);}}}}}return max;}};
accept
0 0
- LeetCode 11. Container With Most Water
- [LeetCode]11.Container With Most Water
- LeetCode --- 11. Container With Most Water
- LeetCode 11.Container With Most Water
- [Leetcode] 11. Container With Most Water
- [leetcode] 11.Container With Most Water
- 【leetcode】11. Container With Most Water
- [LeetCode]11. Container With Most Water
- 11. Container With Most Water LeetCode
- leetcode 11. Container With Most Water
- leetcode 11.Container With Most Water
- [LeetCode OJ]11. Container With Most Water
- [leetcode] 11. Container With Most Water
- Leetcode 11. Container With Most Water
- leetcode 11.Container With Most Water
- leetcode 11. Container With Most Water
- LeetCode 11. Container With Most Water
- LeetCode *** 11. Container With Most Water
- 【剑指 offer】(十三)—— 在 O(1) 时间删除链表结点
- PHP MVC架构
- UVa 201 Squares 枚举
- BUY LOW, BUY LOWER
- C++实现——求三角形的面积(海伦公式)
- leetcode 11. Container With Most Water
- 动画文件的常用格式
- MySQL--基本内容
- android 开启本地相册选择图片并返回显示
- 计算机图形学(一) 视频显示设备_5_平板显示器
- 《算法(第四版)》排序-----堆排序
- imageloder显示参数配置
- mybatis连载(2)
- Ivan and Powers of Two