首页 > 其他分享 >11. Container With Most Water [Medium]

11. Container With Most Water [Medium]

时间:2023-01-10 19:33:42浏览次数:39  
标签:11 right container int height Water Most result left

217. 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.

Constraints:

  • n == height.length
  • 2 <= n <= 10^5
  • 0 <= height[i] <= 10^4

Example
image

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.

思路

求最大面积,那影响面积的两个条件就是长和宽,宽就是数组的值,长就是元素之间相差的位数
其中决定宽的应该是一对值中较小的那个值,大的那个值能适配的肯定更多些,所以就移动小的那一位

题解

  • 无脑快速AC
    public int maxArea(int[] height) {
        int result, left, right, step;
        result = left = 0;
        right = height.length - 1;
        while (left < right) {
	    // 找宽
            int len = Math.min(height[left], height[right]);
	    // 算长
            step = right - left;
            result = Math.max(len * step, result);

	    // 这里要把移位放最后做,不然会直接跳过第一位,其中也会有左边等于右边的情况,这里就让左边移动了
            if (height[left] > height[right])
                right--;
            else
                left++;
        }
        return result;
    }

标签:11,right,container,int,height,Water,Most,result,left
From: https://www.cnblogs.com/tanhaoo/p/17041184.html

相关文章

  • 11-FP-growth算法
    title:11-FP-growth算法date:2021-01-1810:58:30permalink:/pages/e1a7c0/......
  • 11. 盛水最多的容器
    问题描述https://leetcode.cn/problems/container-with-most-water/description/解题思路首先,我们考虑暴力法。暴力法是O(n的平方). 然后对暴力法进行优化。我们发现......
  • 【CF1119H】Triple
    【CF1119H】TripleDescription给出\(n\)个三元组\(a_i,b_i,c_i\),范围为\([0,2^m-1]\)再给出参数\(x,y,z\),每个三元组有\(x\)个\(a_i\),\(y\)个\(b_i\),\(z\)个\(c_i\)对......
  • #110警察节# 110,三个简单的数字,组成了一串安全密码
    ​​#110警察节#​​110,三个简单的数字,组成了一串安全密码:它是联结警民之情的爱心桥,是守护人民安全的生命线,更是新征程下传承公安事业的接力棒。在第三个“中国人民警察节......
  • Educational Codeforces Round 114 D(搜索剪枝)
    D.TheStrongestBuild题目大意:给定n个位置,每个位置有c\({_i}\)个可选能力值(能力值升序给出即a\({_1}\)<a\({_2}\)<a\({_3}\)<...<a\({_{ci}}\)),你可以在每个......
  • windows 11安装oracle 19c客户端
    文档课题:windows11安装oracle19c客户端.软件包:WINDOWS.X64_193000_client.zip--以管理员身份双击setup.exe参考网址:https://cdn.modb.pro/db/580252......
  • luogu P1971 [NOI2011] 兔兔与蛋蛋游戏
    题面传送门没想到二分图博弈这么早就考了?首先我们发现,如果将和起点行列之和奇偶性一样的点设为黑色,其余设为白色,那么每次空格只会在异色边之间走,而不会在同色边之间走。......
  • 「ZJOI2011」道馆之战
    「ZJOI2011」道馆之战给定一棵树,每一个节点\(2\)个房间,可以为薄冰或者障碍物,给定两个操作:修改某一个房间的两个区域,查询从\(u\)走到\(v\)最短可以经过多少薄冰(最终......
  • MySQL主从异常Coordinator stopped because there were error(s) in the worker(s). T
    问题原因gtid方式主从数据不一致,同步数据时报错。mysql>showslavestatus\G***************************1.row***************************Slave_IO_St......
  • VM虚拟机安装windows11最大的两个棘手问题
    VM虚拟机安装windows11最大的两个棘手问题一个是安装过程,显示啥不安全没加密,十成需要虚拟机windows11设置里添加一个可信平台模块,就可以还有一个就是,安装进去,设置windows......