Skip to content

Commit 58174c1

Browse files
committed
修改文件: DynamicProgram/121-best-time-to-buy-and-sell-stock.md
1 parent 63abc15 commit 58174c1

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

DynamicProgram/121-best-time-to-buy-and-sell-stock.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,23 @@ time out
2424
result[i] = max(result[i],result[j] + prices[i] - prices[j]);
2525
return *max_element(result.begin(),result.end());
2626
}
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+
2746
```

0 commit comments

Comments
 (0)