8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d9c85c4 commit 282d670Copy full SHA for 282d670
algorithms/BestTimeToBuyAndSellStockII/README.md
@@ -47,4 +47,25 @@ class Solution(object):
47
max_profit += prices[i] - prices[i - 1]
48
49
return max_profit
50
+```
51
+or use Dynamic Programming like below:
52
+```python
53
+class Solution(object):
54
+ def maxProfit(self, prices):
55
+ """
56
+ :type prices: List[int]
57
+ :rtype: int
58
59
+ if len(prices) == 0:
60
+ return 0
61
+ # profit of not hold stock in i
62
+ not_holder = 0
63
+ # profit of hold stock in i, of course it's -prices[0] when i = 0
64
+ holder = -prices[0]
65
+
66
+ for i in xrange(1, len(prices)):
67
+ not_holder = max(not_holder, prices[i] + holder)
68
+ holder = max(holder, not_holder - prices[i])
69
70
+ return max(holder, not_holder)
71
```
0 commit comments