Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Example 1:
Input: [7, 1, 5, 3, 6, 4]
Output: 5
max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)
Example 2:
Input: [7, 6, 4, 3, 1]
Output: 0
In this case, no transaction is done, i.e. max profit = 0.
首先想到的遍历的方法,记录序列中每个元素与其后面每个元素的差值,找出最大值
代码
class Solution {
public:
int maxProfit(vector<int>& prices) {
int maxProfit = 0;
if(prices.size() <= 1)
return 0;
int temp;
for(int i = 0; i < prices.size() - 1; i ++){
for(int j = i + 1; j < prices.size(); j ++){
temp = prices[j] - prices[i];
if(temp > maxProfit)
maxProfit = temp;
}
}
return maxProfit;
}
};
测试用例通过199/200,还是存在超时问题
看到discuss里面的代码(只一次遍历):
int maxProfit(vector<int>& prices) {
int min = INT32_MAX, profit = 0;
for (int i = 0; i < prices.size(); i++) {
if (prices[i] <= min) min = prices[i];
else profit = max(profit, prices[i] - min);
}
return profit;
}
接着就该问题搜索了一下,发现关于这个问题变种很多,这里列举一下:
Best Time to Buy and Sell Stock II
Say you have an array for which the i th element is the price of a given stock on day i .
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
题目链接:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
此题和上面一题的不同之处在于不限制交易次数。也是一次遍历即可,只要可以赚就做交易。
public class Solution {
public int maxProfit(int[] prices) {
if(prices.length == 0) return 0;
int ans = 0;
for(int i=1; iif(prices[i] > prices[i-1])
ans += prices[i]-prices[i-1];
}
return ans;
}
}
Best Time to Buy and Sell Stock III
Say you have an array for which the i th element is the price of a given stock on day i .
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
题目链接:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/
此题是限制在两次交易内,相对要难一些。容易想到的解决办法是,把prices[] 分成两部分prices[0…m] 和 prices[m…length] ,分别计算在这两部分内做交易的做大收益。由于要做n次划分,每次划分可以采用 第一题: Sell Stock I的解法, 总的时间复杂度为O(n^2).
public class Solution {
public int maxProfit(int[] prices) {
int ans = 0;
for(int m = 0; mint tmp = maxProfitOnce(prices, 0, m) + maxProfitOnce(prices, m, prices.length-1);
if(tmp > ans) ans = tmp;
}
return ans;
}
public int maxProfitOnce(int[] prices,int start, int end){
if(start >= end) return 0;
int low = prices[start];
int ans = 0;
for(int i=start+1; i<=end; i++){
if(prices[i] < low) low = prices[start];
else if(prices[i] - low > ans) ans = prices[i] - low;
}
return ans;
}
}
但是由于效率过低,运行超时。可以利用动态规划的思想进行改进,保持计算的中间结果,减少重复的计算。
那就是第一步扫描,先计算出子序列[0,…,i]中的最大利润,用一个数组保存下来,那么时间是O(n)。计算方法也是利用第一个问题的计算方法。 第二步是逆向扫描,计算子序列[i,…,n-1]上的最大利润,这一步同时就能结合上一步的结果计算最终的最大利润了,这一步也是O(n)。 所以最后算法的复杂度就是O(n)的。
就是说,通过预处理,把上面的maxProfitOnce()函数的复杂度降到O(1)
public class Solution {
public int maxProfit(int[] prices) {
if(prices.length == 0) return 0;
int ans = 0;
int n = prices.length;
//正向遍历,opt[i]表示 prices[0...i]内做一次交易的最大收益.
int opt[] = new int[n];
opt[0] = 0;
int low = prices[0];
int curAns = 0;
for(int i = 1; iif(prices[i] < low) low = prices[i];
else if(curAns < prices[i] - low) curAns = prices[i] - low;
opt[i] = curAns;
}
//逆向遍历, opt[i]表示 prices[i...n-1]内做一次交易的最大收益.
int optReverse[] = new int[n];
optReverse[n - 1] = 0;
curAns = 0;
int high = prices[n - 1];
for(int i=n-2; i>=0; i--){
if(prices[i] > high) high = prices[i];
else if(curAns < high - prices[i]) curAns = high - prices[i];
optReverse[i] = curAns;
}
//再进行划分,分别计算两个部分
for(int i=0; iint tmp = opt[i] + optReverse[i];
if(ans < tmp) ans = tmp;
}
return ans;
}
}