It is very famous problem on LeetCode, which took a while for me to understand and solve, and It’s asked by Fang Company many…