Microsoft interview question

leetcode 797 , Leetcode 752