At first, I had the same idea with you. But I think this in worst case would take o(mn) which is some like [(9,8,7),6,5,4,3,2,1], we have to find max every time. However, if you use a BST to save, that would take o(mlog(n)) and if deque, it would cost o(n);