121. 买卖股票的最佳时机
暴力方法实现:记录每一次的买卖,记录最大的数值并返回
class Solution {
public int maxProfit(int[] prices) {
int pay = Integer.MAX_VALUE;
int max = 0;
for(int i = 0;i<prices.length;i++){
if(prices[i]<pay){
pay = prices[i];
}
max = Math.max(max,(prices[i]-pay));
}
return max;
}
}
动态规划实现:
class Solution {
public int maxProfit(int[] prices) {
int length = prices.length;
int[][] dp = new int[length][2];
int result = 0;
dp[0][0] = -prices[0];
dp[0][1] = 0;
for (int i = 1; i < length; i++) {
dp[i][0] = Math.max(dp[i - 1][0], -prices[i]);
dp[i][1] = Math.max(dp[i - 1][0] + prices[i], dp[i - 1][1]);
}
return dp[length - 1][1];
}
}
122. 买卖股票的最佳时机 II
贪心实现:保证每一天的利润都大于0,总体利润最大
class Solution {
public int maxProfit(int[] prices) {
int profit = 0;
for(int i = 1;i< prices.length;i++){
profit += Math.max(prices[i]-prices[i-1],0);
}
return profit;
}
}
动态规划实现:
class Solution
public int maxProfit(int[] prices) {
int n = prices.length;
int[][] dp = new int[n][2];
dp[0][0] = -price[i];
dp[0][1] = 0
for (int i = 1; i < n; ++i) {
dp[i][0]=Math.max(dp[i-1][0],dp[i-1][1]-prices[i]);
dp[i][1]=Math.max(dp[i-1][1],dp[i-1][0]+prices[i]);
}
return dp[n - 1][1];
}
}
123. 买卖股票的最佳时机 III
class Solution {
public int maxProfit(int[] prices) {
int[][] dp = new int[prices.length][5];
dp[0][0] = 0;
dp[0][1] = -prices[0];
dp[0][2] = 0;
dp[0][3] = -prices[0];
dp[0][4] = 0;
for(int i=1;i<prices.length;i++){
dp[i][1]=Math.max(dp[i-1][1],dp[i-1][0]-prices[i]);
dp[i][2]=Math.max(dp[i-1][2],dp[i-1][1]+prices[i]);
dp[i][3]=Math.max(dp[i-1][3],dp[i-1][2]-prices[i]);
dp[i][4]=Math.max(dp[i-1][4],dp[i-1][3]+prices[i]);
}
return dp[prices.length-1][4];
}
}
188. 买卖股票的最佳时机 IV
除去0以外,偶数就是卖出,奇数就是买入
for (int i = 1; i < 2 * k; i += 2) {
dp[0][i] = -prices[0];
}
class Solution {
public int maxProfit(int k, int[] prices) {
int len = prices.length;
int[][] dp = new int[len][2 * k+1];
//初始化
for (int i = 1; i < 2 * k; i += 2) {
dp[0][i] = -prices[0];
}
for (int i = 1; i < len; i++) {
for (int j = 0; j < k*2 - 1; j += 2) {
dp[i][j + 1] = Math.max(dp[i - 1][j + 1], dp[i - 1][j] - prices[i]);
dp[i][j + 2] = Math.max(dp[i - 1][j + 2], dp[i - 1][j + 1] + prices[i]);
}
}
return dp[len - 1][k*2];
}
}
309. 买卖股票的最佳时机含冷冻期
在持有股票阶段分为三种情况,第一次持有,在冻结后的第一天买入,在冻结后的几天后再买入
class Solution {
public int maxProfit(int[] prices) {
/**
* dp[i][0]:持有股票
* dp[i][1]:保持卖出股票
* dp[i][2]:卖出股票
* dp[i][3]:冻结
*/
//初始化
int[][] dp = new int[prices.length][4];
dp[0][0] = -prices[0];
dp[0][1] = 0;
dp[0][2] = 0;
dp[0][3] = 0;
for (int i = 1; i < prices.length; i++) {
//3种情况:第一次买入,冷冻期后买入,前一天持有
dp[i][0] = Math.max(dp[i - 1][0], Math.max(dp[i - 1][3] - prices[i], dp[i - 1][1] - prices[i]));
//2种情况:一直保持卖出,前一天使冷冻期
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][3]);
dp[i][2] = dp[i - 1][0] + prices[i];
dp[i][3] = dp[i - 1][2];
}
return Math.max(dp[prices.length-1][3],Math.max(dp[prices.length-1][2],dp[prices.length-1][1]));
}
}
714. 买卖股票的最佳时机含手续费
这题与122. 买卖股票的最佳时机 II 思路完全一致,只是在卖出后减去手续费即可
class Solution {
public int maxProfit(int[] prices, int fee) {
//dp[i][0]持有股票 dp[i][1]不持有股票
int len = prices.length;
int[][] dp = new int[len][2];
dp[0][0] = -prices[0];
for(int i = 1;i<len;i++){
dp[i][0]=Math.max(dp[i-1][0],dp[i-1][1]-prices[i]);
dp[i][1]=Math.max(dp[i-1][1],dp[i-1][0]+prices[i]-fee);
}
return dp[len-1][1];
}
}