Click here to see the full article post
Very smart solution! Could you tell me how you came up with this solution? Is there any other resources or questions that can help me build up this knowledge/idea? Thanks.
I feel this is semantically incorrect.
cash has been updated before
hold = max(hold, cash - prices[i]). But luckily if
cash comes from the previous
cash, it's fine. If
cash is from
hold + prices[i] - fee, then in
hold = max(hold, cash - prices[i]),
cash - prices[i] equals
hold + prices[i] - fee - prices[i] which equals
hold - fee, which is always smaller than
hold, leading to a correct result. This is tricky and misleading. It would be better to use
I think explaining optimal substructure and overlapping sub-problem would help vastly. its also unclear how formulas of cash and hold are arrived at
@kenan3 LeetCode has several problems that are variations of this one. It should be obvious after you complete those:
@benjamin19890721 I have completed buy and sell stock 1 and 2, and jump to this question. I found the way to solve this question is very different with those two questions, but it does look similar with buy and sell stock 3. I will figure it out. Thanks!
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.