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;
    }
};
相关推荐
普通网友12 分钟前
C++中的代理模式实战
开发语言·c++·算法
普通网友42 分钟前
C++模块化设计原则
开发语言·c++·算法
864记忆44 分钟前
Qt c++的基础语法有哪些?
开发语言·c++·qt
倦王1 小时前
力扣日刷251117
算法·leetcode·职场和发展
龙泉寺天下行走1 小时前
Vscode 配置C++ Mingw调试、编译环境-无需修改系统PATH变量的VS Code配置方法
c++·ide·vscode
AA陈超1 小时前
ASC学习笔记0025:移除所有属性集
c++·笔记·学习·ue5·虚幻引擎
Genevieve_xiao1 小时前
【数据结构】【xjtuse】八股文单元小测
数据结构·算法
QT 小鲜肉1 小时前
【Linux常用命令大全】在 Linux 系统下 Git + Vim编辑器常用指令完全指南(亲测有效)
linux·开发语言·c++·笔记·git·编辑器·vim
Xの哲學1 小时前
Linux slab分配器深度剖析:从原理到实践
linux·服务器·算法·架构·边缘计算
oioihoii2 小时前
现代C++:一场静默的革命,告别“C with Classes”
c语言·jvm·c++