We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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:
Let's define the square to be rotated by its top left & button right corners
Let's say top left corner is at index (strtRow, strtCol)
Buttom right corner is at index (endRow, endCol)
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)
If cell (i, j) is outside the square to be rotated, it will stay at (i, j)
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.
If cell (i, j) is outside some square, it's definitely outside all its next squares
If cell (i, j) is rotated by some square, it's definitely rotated by all its previous squares
We can take overall effect of each prefix set of rotations using point 4
For every cell to be searched for, find the last rotation contains the target cell, then get final position of that cell
Finding last rotation contains the target call can be done with aid of observations 6 & 7 and binary search
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
King Richard's Knights
You are viewing a single comment's thread. Return to all 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: