I used to think this problem was really hard, but the acceptance rate was so high. I was frustrated wondering why I couldn’t solve it.
Then yesterday, after solving Part I on a whim, everything suddenly clicked.
The problem requires that you must buy before you sell, and you can only buy again after selling. You can make multiple transactions. What is the maximum profit?
The solution: compute the day-to-day price differences, then sum up all the positive differences.
Accepted on the first try:
| |