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;
    }
};
相关推荐
lifallen3 小时前
Hadoop MapReduce 任务/输入数据 分片 InputSplit 解析
大数据·数据结构·hadoop·分布式·算法
熙xi.4 小时前
数据结构 -- 哈希表和内核链表
数据结构·算法·散列表
Ghost-Face4 小时前
并查集提高——种类并查集(反集)
算法
董董灿是个攻城狮5 小时前
5分钟搞懂大模型微调的原始能力退化问题
算法
Univin5 小时前
8.25作业
数据结构·windows
Incredibuild5 小时前
DevSecOps 集成 CI/CD Pipeline:实用指南
c++·ci/cd·devsecops
胡萝卜3.07 小时前
数据结构初阶:详解单链表(一)
数据结构·笔记·学习·单链表
君鼎8 小时前
More Effective C++ 条款01:仔细区别 pointers 和 references
c++
艾醒8 小时前
大模型面试题剖析:大模型微调与训练硬件成本计算
人工智能·后端·算法