eBay Interview Question

It is a DP problem for LC, probably LC medium difficulty.