Tile interview question

Leetcode medium problem