<style> html, body, .ui-content { background: #222222; color: #00BFFF; } ::-webkit-scrollbar { width: 10px; } ::-webkit-scrollbar-track { background: transparent; } ::-webkit-scrollbar-thumb { background: linear-gradient(180deg, #2BE8CF60 0%, #2B83E860 100%); border-radius: 3px; } ::-webkit-scrollbar-thumb:hover { background: linear-gradient(180deg, #2BE8CF95 0%, #2B83E895 100%); } /* 設定 code 模板 */ .markdown-body code, .markdown-body tt { background-color: #ffffff36; } .markdown-body .highlight pre, .markdown-body pre { color: #ddd; background-color: #00000036; } .hljs-tag { color: #ddd; } .token.operator { background-color: transparent; } </style> ###### tags: `Leetcode` # 122. Best Time to Buy and Sell Stock II ###### Link : https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/ ## 題目 找到最大獲利(最多同時買一項,但可在同一天賣掉並馬上買) ## 程式碼 ```cpp= class Solution { public: int maxProfit(vector<int>& prices) { int buy = prices[0], profit = 0; for(int i = 0;i < prices.size();++i){ if(prices[i] > buy){ profit += prices[i] - buy; buy = prices[i]; } buy = min(buy, prices[i]); } return profit; } }; ```