标签:name array 看到了 fit design 查找 ack multi article
int maxProfitIII(vector<int> &prices) { int len = prices.size(); if (len == 0) return 0; vector<int> history(len, 0); vector<int> future(len, 0); int low = prices[0]; for (int i = 1; i < len; i++) { history[i] = max(history[i-1], prices[i] - low); low = min(low, prices[i]); } int high = prices[len-1]; for (int i = len-2; i >= 0; i--) { future[i] = max(future[i+1], high - prices[i]); high = max(high, prices[i]); } int maxProfit = 0; // 网上说第一次的卖和第二次的买能够在同一次。所以直接都是i相加就能够了 for (int i = 0; i < len; i++) { maxProfit = max(maxProfit, history[i] + future[i]); } return maxProfit; }
Best Time to Buy and Sell Stock III
标签:name array 看到了 fit design 查找 ack multi article
原文地址:http://www.cnblogs.com/llguanli/p/7352856.html