TikTok Interview Question

leetcode 1863 and leetcode 76.