Transform to Chessboard


  • 0

    Click here to see the full article post


  • 0
    P

    Thanks a lot!

    int ones = Integer.bitCount(k1 & Nones);

    Is "k1 & Nones" necessary here?


  • 0
    L

    if (!(count.get(k1) == N/2 && count.get(k2) == (N+1)/2) &&
    !(count.get(k2) == N/2 && count.get(k1) == (N+1)/2))
    return -1;

    can be simplified to :
    if (Math.abs(count.get(k1)-count.get(k2))>1){
    return -1;
    }


  • 0
    D

    nice solution.

    i think if each row and each column is half ones or half zeros (or with an extra 1/0 when odd length) then it is impossible for there to be more or less than 2 distinct rows/columns. therefore those checks are redundant


  • 0
    A

    Shouldn't the time complexity be O(n)? After all, all the pre-checks are linear, and in order to find the ideal number of steps we just need to count the number of incorrectly placed rows or columns which is also linear. Can someone explain why is it O(n^2). Thanks a lot in advance.


  • 0
    F

    Great solution! Very clear explanation!


  • 0
    R

    Shouldn't 0xAAAAAAAA and 0x55555555 be swapped, because if it starts with zero, it should be checked with 0x55555555 and when it starts with 1, it should be checked with 0xAAAAAAAA....please correct me if I am wrong


  • 0
    R

    Ohh I got it!!!... Thanks, great solution, takes some time to understand, but the flow is clearly explained.


Log in to reply
 

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