Google interview question

Leetcode 113 but with variation