3ms o(n) java solution


  • 3
    L
    public class Solution {
    public int maxArea(int[] height) {
        int maxarea=0;
        int temparea=0;
        int m=0,n=height.length-1;
        while(m!=n){
            if(height[m]<height[n]){
                temparea=height[m]*(n-m);
                m++;
            }
            else{
                temparea=height[n]*(n-m);
                n--;
            }
            if(maxarea<temparea) maxarea=temparea;
        }
        return maxarea;
    }
    

    }


  • 0
    X

    I've found a problem. Replacing "while (m != n)" by "while(m < n)" would make the running time exceed limit under the test case from 15,000 to 1, although theoretically they are the same.
    The error also occurs when I run another solution posted on https://leetcode.com/discuss/73548/simple-and-easy-understanding-java-solution.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.