Please feedback me about my coding

  • 0

    Hello. I finished my style of coding as below and I see the result is different to the one that leetCode offers.
    I see that leetCode has different assumption that I have :
    the points at x-axis starts from 0~ height.length-1.

    As the question stated "Given n non-negative integers a1, a2, ..., an",
    the i starts from 1 not 0.
    That is why as below, I assumed that the point starts at least from (1,XX) not (0,XX).
    Due to this assumption of mine, the container will always shape as trapezoid
    so eventually the maxarea has to be divided by 2 in the end.
    Let me know what you guys think and it would be great if you could write feedback!
    Thank you.

    public class Solution {
    public int maxArea(int[] height) {
    //find two lines? then why the return statement is just int?
    //Input: [0,2], Output : 0 -> because there cannot be two lines?
    int maxarea=0;
    int area=0;

        for (int i=1;i<=height.length;i++) {
            for (int j=i+1;j<=height.length;j++) {
               if (height[i-1] == 0 || height[j-1] ==0) {
                   return 0; //because there cannot be two lines?
               area = ((height[i-1]+height[j-1])*(j-i))/2; //should also have /2 but I am afraid it will return not int
               System.out.println("height[i-1] : " + height[i-1]);
               System.out.println("height[j-1] : " + height[j-1]);
               System.out.println("j, i, j-i: " + j + "," + i + ","+ (j-i));
               System.out.println("area : " + area);
               if(area > maxarea) {
                   maxarea= area;
        return maxarea;


Log in to reply

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