zoukankan      html  css  js  c++  java
  • 11. Container With Most Water(装最多的水 双指针)

    Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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 and n is at least 2.

    l = 0 

    r = n-1

    a[l] <a[r]

    l++

    那边矮扔掉哪边

     1 class Solution {
     2 public:
     3     int maxArea(vector<int>& a) {
     4         const int n = a.size();
     5         int res = 0;
     6         int low = 0;
     7         int high = n-1;
     8         while(low<=high) {
     9             res = std::max((high-low)*std::min(a[low],a[high]),res);
    10             if(a[low]<a[high]) {
    11                 low++;
    12             } else {
    13                 high--;
    14             }
    15         }
    16         return res;
    17     }
    18 };
     1 class Solution:
     2     def maxArea(self, a):
     3         """
     4         :type height: List[int]
     5         :rtype: int
     6         """
     7         maxArea = 0
     8         l = 0
     9         r = len(a) - 1
    10         while(l<r):
    11             maxArea = max(maxArea,min(a[l],a[r])*(r-l))
    12             if(a[l]<a[r]):
    13                 l+=1
    14             else :
    15                 r-=1
    16         return maxArea
    17             
  • 相关阅读:
    CopyOnWriteArrayList源码阅读笔记
    ArrayList源码阅读笔记
    MySQL和Oracle的区别
    思维导图概览SpringCloud
    Java学习之Request篇
    Java学习之servlet篇
    Java学习之数据库连接池
    Java学习之注解篇
    Java爬取先知论坛文章
    Java学习之爬虫篇
  • 原文地址:https://www.cnblogs.com/zle1992/p/8691492.html
Copyright © 2011-2022 走看看