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, height[i]).

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 <= height[i] <= 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;
    }
};
相关推荐
NAGNIP7 小时前
轻松搞懂全连接神经网络结构!
人工智能·算法·面试
NAGNIP7 小时前
一文搞懂激活函数!
算法·面试
董董灿是个攻城狮7 小时前
AI 视觉连载7:传统 CV 之高斯滤波实战
算法
爱理财的程序媛13 小时前
openclaw 盯盘实践
算法
端平入洛14 小时前
auto有时不auto
c++
MobotStone17 小时前
Google发布Nano Banana 2:更快更便宜,图片生成能力全面升级
算法
颜酱20 小时前
队列练习系列:从基础到进阶的完整实现
javascript·后端·算法
用户57573033462420 小时前
两数之和:从 JSON 对象到 Map,大厂面试官到底在考察什么?
算法
程序猿追20 小时前
“马”上行动:手把手教你基于灵珠平台打造春节“全能数字管家”
算法