4 ms·
Yes, there is a natural way to solve these recursively. Here is an example: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/submissions/114651
by tylerhou 3y ago
Yes, there is a natural way to solve these recursively. Here is an example: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/submissions/1146512107 https://leetcode.com/problems/best-time-to-buy-and-sell-stoc...
Not the best runtime (I could use a multidimensional array instead of an hash map for the cache, and recursion is "slow" in Python), but it's a clear solution.
Given a recursive solution, it is also "trivial" to convert it into filling an array by visiting the recursive states in reverse topological order. So here is a submission that fills in the array: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/submissions/1146519236 https://leetcode.com/problems/best-time-to-buy-and-sell-stoc...
- deleted 3y ago[deleted]