King Richard's Knights

  • + 3 comments

    I just solved this problem, but in a different way than editorial. Time complicity is O(L log S) same as editorial.

    My approach was based on these points:

    1. Let's define the square to be rotated by its top left & button right corners
    2. Let's say top left corner is at index (strtRow, strtCol)
    3. Buttom right corner is at index (endRow, endCol)
    4. If cell (i, j) is inside the square to be rotated, it can be proven that the new index for that cell will be (strtRow - strtCol + j, endCol + strtRow - i)
    5. If cell (i, j) is outside the square to be rotated, it will stay at (i, j)
    6. We can know if a cell is inside/outside the square by assuming it's inside the square and finding its new position. If the result position is inside the square, then the original position is inside the square. Otherwise, it's outside the square.
    7. If cell (i, j) is outside some square, it's definitely outside all its next squares
    8. If cell (i, j) is rotated by some square, it's definitely rotated by all its previous squares
    9. We can take overall effect of each prefix set of rotations using point 4
    10. For every cell to be searched for, find the last rotation contains the target cell, then get final position of that cell
    11. Finding last rotation contains the target call can be done with aid of observations 6 & 7 and binary search