题目描述

给定n个非负整数a1,a2,…,an,其中每个数字表示坐标(i, ai)处的一个点。以(i,ai)和(i,0)(i=1,2,3...n)为端点画出n条直线。你可以从中选择两条线与x轴一起构成一个容器,最大的容器能装多少水?
注意:你不能倾斜容器
例如:
输入 [1,8,6,2,5,4,8,3,7]
输出: 49

Given n non-negative integers a1 , a2 , ..., an , where each represents a point at coordinate (i, ai ). nvertical lines are drawn such that the two endpoints of line i is at (i, ai ) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container.
Example:
Input: [1,8,6,2,5,4,8,3,7]
Output: 49
示例1

输入

复制

[1,8,6,2,5,4,8,3,7]

输出

复制

49

class Solution {
public:
    /**
     *
     * @param height int整型vector
     * @return int整型
     */
    int maxArea(vector<int>& height) {
        // write code here
        if (height.size()<2)//数据太少不能构成容器
            return 0;
        int i=0,j=height.size()-1;
        int maxArea=0;
        int tmpArea=0;
        while (i<j){
            tmpArea=min(height[i],height[j])*(j-i);
            if (tmpArea>maxArea)
                maxArea=tmpArea;
            if (height[i]<height[j])
                ++i;
            else
                --j;
        }
        return maxArea;
    }
};
class Solution {
public:
    /**
     *
     * @param height int整型vector
     * @return int整型
     */
    int maxArea(vector<int>& height) {
        // write code here
        int l,r,Max=-9999;
        for (l=0,r=height.size()-1;l<r;){
            Max=max(Max,(r-l)*min(height[l],height[r]));
            height[l]<height[r]?l++:r--;
            
            
        }
        return Max;
    }
};
import java.util.*;

public class Solution {
    /**
     *
     * @param height int整型一维数组
     * @return int整型
     */
    public int maxArea (int[] height) {
        // write code here
        if (height.length<2){
            return 0;
            
        }
        int left=0;
        int right=height.length-1;
        int maxV=0;
        while (left<right){
            int v=Math.min(height[left],height[right])*(right-left);
            maxV=Math.max(v,maxV);
            if (height[left]<height[right]){
                left++;
            }else {
                right--;
            }
        }
        return maxV;
    }
}
#
#
# @param height int整型一维数组
# @return int整型
#
class Solution:
    def maxArea(self , height ):
        # write code here
        left=0
        right=len(height)-1
        V=0
        while left<right:
            V=max(V,min(height[left],height[right])*(right-left))
            if height[left]<height[right]:
                left+=1
            else:
                right-=1
        return V
            
            
          

leetcode138container-with-water的更多相关文章

  1. [LeetCode] Pacific Atlantic Water Flow 太平洋大西洋水流

    Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...

  2. [LeetCode] Trapping Rain Water II 收集雨水之二

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  3. [LeetCode] Water and Jug Problem 水罐问题

    You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...

  4. [LeetCode] Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  5. [LeetCode] Container With Most Water 装最多水的容器

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...

  6. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

  7. 【leetcode】Container With Most Water

    题目描述: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...

  8. [LintCode] Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  9. [LintCode] Container With Most Water 装最多水的容器

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  10. LeetCode#11. Container With Most Water

    问题描述 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...

随机推荐

  1. MeteoInfoLab脚本示例:TOMS HDF数据

    TOMS (Total Ozone Mapping Spectrometer)数据是全球臭氧观测.脚本程序: #Add data file folder = 'D:/Temp/hdf/' fns = ...

  2. Django基础之Ajax

    知识预览 Ajax前戏:json Ajax简介 Jquery实现的ajax JS实现的ajax Ajax前戏:json 什么是json? 定义: JSON(JavaScript Object Nota ...

  3. 【不知道怎么分类】CF 819B Mister B and PR Shifts

    题目内容 洛谷链接 定义一个全排列\(p_i\)的偏移值为\(\sum_{i=1}^{n}|p[i]-i|\). 给你一个全排列,你可以从后面拿\(k\in[0,n-1]\)个数放在前面,使得该全排列 ...

  4. 【博弈论】CF 1215D Ticket Game

    题目大意 洛谷链接 给出一个长度为\(n\)的由数字组成的字符串(\(n\)是偶数).但可能有偶数个位上的数字为?. 现在有两个人\(A\)和\(B\),在?的位置上填\(0\)~\(9\)的数,一直 ...

  5. selenium---输入内容后搜索

    from time import sleep from selenium import webdriver br = webdriver.Chrome() url = "https://ww ...

  6. SE第一次作业

    作业一.对软件工程的初步认识 下面是我对于软件工程的认识,结合自己的理解和课上听讲的内容 软件工程=软件+工程?软件工程是否就是简单的软件+工程呢?那么我们先来看下各自的概念. 那么什么叫软件呢,既然 ...

  7. linux centos8 安装dokcker并启动coreapi

    粘的个人笔记,格式有点乱.勿在意 core api程序包 发布直接部署包: 链接:https://pan.baidu.com/s/1zZe9H1Fevf7DdzfF-MJb9w 提取码:t0ai 源码 ...

  8. 记一次flink入门学习笔记

    团队有几个系统数据量偏大,且每天以几万条的数量累增.有一个系统每天需要定时读取数据库,并进行相关的业务逻辑计算,从而获取最新的用户信息,定时任务的整个耗时需要4小时左右.由于定时任务是夜晚执行,目前看 ...

  9. 从一个例子入手Istio

    转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com 本文使用的Istio源码是 release 1.5. 本篇是Istio系列的第一篇,希望 ...

  10. Java基础之类型转换总结篇

    Java中,经常可以遇到类型转换的场景,从变量的定义到复制.数值变量的计算到方法的参数传递.基类与派生类间的造型等,随处可见类型转换的身影.Java中的类型转换在Java编码中具有重要的作用.    ...