Grid Dynamics Interview Question

Solve a question from leetcode