LeetCode75——Day12

文章目录

一、题目

11. Container With Most Water

You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, heighti).

Find two lines that together with the x-axis form a container, such that the container contains the most water.

Return the maximum amount of water a container can store.

Notice that you may not slant the container.

Example 1:

Input: height = 1,8,6,2,5,4,8,3,7

Output: 49

Explanation: The above vertical lines are represented by array 1,8,6,2,5,4,8,3,7. In this case, the max area of water (blue section) the container can contain is 49.

Example 2:

Input: height = 1,1

Output: 1

Constraints:

n == height.length

2 <= n <= 105

0 <= heighti <= 104

二、题解

双指针+贪心

cpp 复制代码
class Solution {
public:
    int maxArea(vector<int>& height) {
        int n = height.size();
        int left = 0,right = n - 1;
        int maxS = 0;
        while(left < right){
            int h1 = height[left];
            int h2 = height[right];
            int S = min(h1,h2) * (right - left);
            maxS = max(maxS,S);
            if(h1 > h2) right--;
            else left++;
        }
        return maxS;
    }
};
相关推荐
磊 子23 分钟前
STL无序关联容器—unorded_set+unorded_map
开发语言·c++
通信小呆呆29 分钟前
Vandermonde结构及其快速算法详解
线性代数·算法
初夏睡觉33 分钟前
数据结构学习之~二叉堆 (P3378 【模版】堆)
数据结构·c++·学习
云泽8081 小时前
笔试算法 - 链表篇(一):移除、反转、合并、回文判断全解析
数据结构·c++·算法·链表
也曾看到过繁星1 小时前
数据结构-复杂度
数据结构
菜菜的顾清寒1 小时前
HOT力扣100(43)二叉树-翻转二叉树
数据结构·算法·leetcode
通信小呆呆1 小时前
Toeplitz结构及其快速算法详解
算法
小poop1 小时前
深入理解指针(中):数组与指针的进阶之旅
c++
YikNjy1 小时前
break和continue
java·开发语言·算法