股票(一次交易)
面试题63.:假设把某股票的价格按照时间先后顺序存储在数组中,请问买卖该股票一次可能获得的最大利润是多少?
示例:
输入: [7,1,5,3,6,4]
输出: 5
解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格。
思路分析(暴力解)
暴力解法,两次循环:
class Solution {
public:
int maxProfit(vector<int>& prices) {
int maxProfitVal = 0;
for (int i = 0; i < prices.size(); i++) {
for (int j = i + 1; j < prices.size(); j++) {
if (prices[j] < prices[i]) {
continue;
}
maxProfitVal = max(maxProfitVal, prices[j] - prices[i]);
}
}
return maxProfitVal;
}
};
上述方法在leetcode超时,改成下面后可以通过:
class Solution {
public:
int maxProfit(vector<int>& prices) {
int maxProfitVal = 0;
int n = static_cast<int>(prices.size());
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
maxProfitVal = max(maxProfitVal, prices[j] - prices[i]);
}
}
return maxProfitVal;
}
};
可能原因是prices.size()比较耗时?2021/9/21
时间复杂度:O(N平方);空间复杂度:O(N)
思路分析(一次遍历)
如果我是在历史最低点买的股票就好了!太好了,在题目中,我们只要用一个变量记录一个历史最低价格 minprice,我们就可以假设自己的股票是在那天买的。那么我们在第 i 天卖出股票能得到的利润就是 prices[i] - minprice。
因此,我们只需要遍历价格数组一遍,记录历史最低点,然后在每一天考虑这么一个问题:如果我是在历史最低点买进的,那么我今天卖出能赚多少钱?当考虑完所有天数之时,我们就得到了最好的答案。
class Solution {
public:
int maxProfit(vector<int>& prices) {
int lowestPrice = 1e9;
int maxProfitVal = 0;
for (auto& price : prices) {
maxProfitVal = max(maxProfitVal, price - lowestPrice);
lowestPrice = min(lowestPrice, price); // 维护一个最小值
}
return maxProfitVal;
}
};
时间复杂度:O(N1);空间复杂度:O(1)
思路三(动态规划)
https://blog.nowcoder.net/n/cc9834c59e04456da9a4b8bc0c5c96df?f=comment
两次交易
思路分析
https://blog.nowcoder.net/n/e8fb8be2723d45cb9e941149abe4d7c0?f=comment
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* 两次交易所能获得的最大收益
* @param prices int整型vector 股票每一天的价格
* @return int整型
*/
int maxProfit(vector<int>& prices) {
// write code here
if (prices.size() == 0) return 0;
/*
5个状态:1)不操作2)第一次购买3)第一次卖出4)第二次购买5)第二次卖出
dp[i][j]代表第i天状态为j时产生的最大收益
*/
int dp[200005][5]={0};
//初始化
dp[0][1] = -prices[0];
dp[0][3] = -prices[0];
for (int i = 1; i < prices.size(); i++) {
dp[i][0] = dp[i - 1][0];
//其中dp[i][1]有两个操作1)第i天没有操作2)第i天买入股票,所以此时最大收益,应该为这两个操作比大小
dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
//其中dp[i][2]有两个操作1)第i天没有操作2)第i天卖出股票,所以此时最大收益,应该为这两个操作比大小
dp[i][2] = max(dp[i - 1][2], dp[i - 1][1] + prices[i]);
//其中dp[i][3]有两个操作1)第i天没有操作2)第i天买入股票,所以此时最大收益,应该为这两个操作比大小
dp[i][3] = max(dp[i - 1][3], dp[i - 1][2] - prices[i]);
//其中dp[i][4]有两个操作1)第i天没有操作2)第i天卖出股票,所以此时最大收益,应该为这两个操作比大小
dp[i][4] = max(dp[i - 1][4], dp[i - 1][3] + prices[i]);
}
return dp[prices.size() - 1][4];
}
};
///空间复杂度的优化版本
int maxProfit(vector<int>& prices) {
// write code here
if (prices.size() == 0) return 0;
/*
5个状态:1)不操作2)第一次购买3)第一次卖出4)第二次购买5)第二次卖出
dp[i][j]代表第i天状态为j时产生的最大收益
*/
//初始化
int dp0 = 0;
int dp1 = -prices[0];
int dp2 = 0;
int dp3 = -prices[0];
int dp4 = 0;
for(int i = 1;i < prices.size();i ++){
dp0 = dp0;
dp1 = max(dp1, dp0 - prices[i]);
dp2 = max(dp2,dp1 + prices[i]);
dp3 = max(dp3,dp2 - prices[i]);
dp4 = max(dp4,dp3 + prices[i]);
}
return dp4;
}
股票(无限次交易)
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* 计算最大收益
* @param prices int整型vector 股票每一天的价格
* @return int整型
*/
int maxProfit(vector<int>& prices) {
// write code here
int ans = 0;
int len = prices.size();
for (int i = 1; i < len; i++) {
if (prices[i] > prices[i - 1]) {
ans += prices[i] - prices[i - 1];
}
}
return ans;
}
};