【LeetCode】11. 盛最多水的容器
2023-09-27 14:29:07 时间
给定一个长度为 n 的整数数组 height 。有 n 条垂线,第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) 。
找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
返回容器可以储存的最大水量。
说明:你不能倾斜容器。
示例 1:
输入:[1,8,6,2,5,4,8,3,7]
输出:49
解释:图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。
示例 2:
输入:height = [1,1]
输出:1
提示:
n == height.length
2 <= n <= 105
0 <= height[i] <= 104
代码实现:
#include <iostream>
#include <assert.h>
#include <algorithm>
using namespace std ;
int a[1000] ;
int main(void)
{
int n ;
cin >> n ;
for(int i=0; i<n; i++)
{
cin >> a[i] ;
}
int sum = 0 ;
int l = 0, r = 100-1 ;
while(r>l)
{
int S = min(a[l],a[r]) * (r-l) ;
sum = max(sum, S) ;
a[r]>a[l]?l++:r-- ;
}
cout << sum << endl ; ;
return 0 ;
}
相关文章
- LeetCode每日一练 —— 21. 合并两个有序链表
- 【LeetCode】课程表 II [M](拓扑排序)
- 【LeetCode】K个逆序对数组 [H](动态规划)
- 【LeetCode】盛最多水的容器 [M](贪心)
- leetcode必刷题:盛最多水的容器
- LeetCode_栈_中等_1003.检查替换后的词是否有效
- LeetCode_前缀树_排序_哈希集合_中等_720.词典中最长的单词
- LeetCode刷题(17)【中等】两数相加(C++)
- LeetCode-122. 买卖股票的最佳时机 II(Goland实现)
- [LeetCode] 316. Remove Duplicate Letters 移除重复字母
- [LeetCode] 11. Container With Most Water 装最多水的容器
- leetcode 105 从前序与中序遍历序列构造二叉树
- leetcode 14 最长公共前缀
- leetcode 90子集II
- leetcode 239 滑动窗口最大值
- leetcode 454 四数相加II