Leetcode 11. Container With Most Water (two pointers)

Leetcode: 11

there are two ways to deal with two pointers

one is O(n), two pointers moves from both side

Another is O(2N), two pointer move from the same side

Idea for this, choose the first one and then if there is a smaller one, change that corresponding pointer until bigger that the prior bigger one.

class Solution {
    public int maxArea(int[] height) {
        //two pointers -> N
        //two pointers -> 2*N
        //6,5,4,7,8,2,4,6,9,
        //1 2 3 4 5 6 7 8 9
        //idea: (i ++) (j--) , for two points pointed by two pointers, there must be one big and one small, change the pointer pointed to small so that mamimize the possibility
        int i = 0;
        int j = height.length-1;
        int max  = 0;
        while(i<j){//if i==j break
            //compute the area
            if(height[i]>height[j]){//i bigger
                int temp = (j-i)*(height[j]);
                if(temp>max) max = temp;
                j--;
            }else {
                int temp = (j-i)*(height[i]);
                if(temp>max) max = temp;
                i++;
            }
        }
        return max;
    }
}

haishi cai

原文地址:https://www.cnblogs.com/stiles/p/leetcode11.html

时间: 2024-08-04 11:41:00

Leetcode 11. Container With Most Water (two pointers)的相关文章

LeetCode 11. Container With Most Water (装最多水的容器)

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical 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 containe

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

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical 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 containe

[leetcode]11. Container With Most Water存水最多的容器

Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical 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 contain

LeetCode 11 Container With Most Water(最大水容器)

翻译 给定n个非负整数a1,a2,...,an,其中每个代表一个点坐标(i,ai). n个垂直线段例如线段的两个端点在(i,ai)和(i,0). 找到两个线段,与x轴形成一个容器,使其包含最多的水. 备注:你不必倾倒容器. 翻译 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such tha

LeetCode 11 Container With Most Water (C,C++,Java,Python)

Problem: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical 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

[LeetCode] 11. Container With Most Water My Submissions Question 解题思路

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical 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 containe

LeetCode 11 Container With Most Water

题目如下: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical 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 co

LeetCode 11 Container With Most Water 装尽可能多的水

题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical 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 cont

[LeetCode]11. Container With Most Water 盛最多水的容器

Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical 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 contain