Lets say that our Tetris board is represented as a 2D array of zeros and ones, where a 0 means empty and 1 means occupied, and you where asked to find the highest row in which a tetromino exists.
Assuming that we didn't keep track of the height that is for every tetramino placed check if its row is > previously placed tetromino, if so then height = row.
Obviously the most inefficient way is to iterate through the whole 2D array and perhaps stop whenever a full row of zeros is encountered. Another approach that I thought of is binary search, where you start at the middle row and check if it's empty then my new interval is limited to rows below, else the row of interests is located top.
- Is this an efficient way of finding height?
- What is a better approach?