###### tags: `LeetCode` `Dynamic Programming` `Medium` # LeetCode #122 [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii) ### (Medium) 給定一個數組 prices ,其中 prices[i] 是一支給定股票第 i 天的價格。 設計一個算法來計算你所能獲取的最大利潤。你可以盡可能地完成更多的交易(多次買賣一支股票)。 注意:你不能同時參與多筆交易(你必須在再次購買前出售掉之前的股票)。 --- 能預測未來, 那就逢低買入, 逢高賣出即可。 --- ``` class Solution { public: int maxProfit(vector<int>& prices) { int profit=0; for(int i=0;i<prices.size()-1;i++){ if(prices[i+1]>prices[i])profit+=prices[i+1]-prices[i]; } return profit; } }; ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up