Google Interview Question

Leetcode 113 but with variation