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;
    }
};
相关推荐
TracyCoder1234 小时前
LeetCode Hot100(15/100)——54. 螺旋矩阵
算法·leetcode·矩阵
u0109272715 小时前
C++中的策略模式变体
开发语言·c++·算法
2501_941837266 小时前
停车场车辆检测与识别系统-YOLOv26算法改进与应用分析
算法·yolo
Aevget6 小时前
MFC扩展库BCGControlBar Pro v37.2新版亮点:控件功能进一步升级
c++·mfc·界面控件
探序基因7 小时前
单细胞Seurat数据结构修改分群信息
数据结构
六义义7 小时前
java基础十二
java·数据结构·算法
四维碎片7 小时前
QSettings + INI 笔记
笔记·qt·算法
Tansmjs7 小时前
C++与GPU计算(CUDA)
开发语言·c++·算法
独自破碎E8 小时前
【优先级队列】主持人调度(二)
算法
weixin_445476688 小时前
leetCode每日一题——边反转的最小成本
算法·leetcode·职场和发展