At first I thought I use the greedy: the move left pointer if the new one has longer height which can counteract the width, and use the same for right pointer. But it works not well, because maybe I set the rule too strictly. And also maybe the consequence to move pointers may arbitrary.
So when I look at the solution, it loose the constrain and move the pointers base on current situation. Instead of first move to left pointer then move right pointer, we choose which one to move base on current location and height, and do not need to check the width, since maybe such local optimal maybe lead to global optimal.
I think theoretically is because in my first attempt, the local optimal cannot lead to global optimal, while the solution can do it.