Why not 13?


  • 0
    H

    [1,2,1,2,1]

    in the case of these numbers, it seems the output should be 13.
    for 3times 1 = 33 = 9
    for 2times 2 = 2
    2 = 4
    9 + 4 = 13.

    but it says expected output is 11.
    can someone please explain it?

        int nbox = boxesSize;
        char a; int i = 0, rn = 0, ans = 0, j;
        while(i < nbox )
        {
            if((*(boxes+i)) != -1)
            {a = (*(boxes+i));
            (*(boxes+i)) = -1;
            rn = 1;
            for(j = 0; j< nbox;j++ )
            {
                if(a == (*(boxes+j)) && (*(boxes+j)) != -1 )
                {rn++; (*(boxes+j)) = -1;}
            }
            ans = ans +( rn * rn);
            rn = 0;}
            i++;
        }
        
        return ans;
    }
    

  • 0

    You should remove contiguous boxes. In this case, 2 and 2 cannot be removed together only except you remove the middle 1 first.


  • 0
    S

    @arifkhan123 said in Why not 13?:

    numbers

    Only the contiguous numbers count. The answer should be
    1+1+3^2 = 11


  • 0
    J

    You can only remove contiguous blocks, while it seems you are getting rid of all the like numbers:

    INVALID: [1,2,1,2,1] -> [1,1,1] -> []
    VALID: [1,2,1,2,1] -> [1,1,2,1] -> [1,1,1] -> []

    in the case I marked as valid, the score would be as follows:

    1*1 + 1*1 + 3*3 = 1+1+9 = 11


  • 0

    @arifkhan123
    After first step, the boxes becomes [1,1,2,1] (or [1,2,1,1]), and the current point is 1*1.
    After second step, the boxes becomes [1,1,1], and the current point is 1*1 + 1*1.
    Then the final step, the boxes becomes [], and the current point is 1*1 + 1*1 + 3*3 == 11.

    Thus yes, the highest point is 11 not 13.


Log in to reply
 

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