We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 63abc15 commit 58174c1Copy full SHA for 58174c1
1 file changed
DynamicProgram/121-best-time-to-buy-and-sell-stock.md
@@ -24,4 +24,23 @@ time out
24
result[i] = max(result[i],result[j] + prices[i] - prices[j]);
25
return *max_element(result.begin(),result.end());
26
}
27
+```
28
+
29
+## solution 2. Dp
30
+```cpp
31
+ int maxProfit(vector<int>& prices) {
32
+ if (prices.empty())
33
+ return 0;
34
+ vector<int> result(prices.size(),0);
35
+ int min_elem = 0;
36
+ for (int i = 1; i < prices.size(); i++)
37
+ {
38
+ if (prices[i] > prices[min_elem])
39
+ result[i] = result[min_elem] + prices[i] - prices[min_elem];
40
+ else
41
+ min_elem = i;
42
+ }
43
+ return *max_element(result.begin(),result.end());
44
45
46
```
0 commit comments