sol 1: use stack. sol 2:DP, not figure out the dp equation