J.P. Morgan interview question

Leetcode Medium, Leetcode Hard question