Electronic Arts Interview Question

A leetcode medium problem supposed to be slove in 20mins.