Leetcode121/122/714/123/309之一口气看完买卖股票问题
Leetcode121-买卖股票的最佳时机
- 给定一个数组
prices
,它的第i
个元素prices[i]
表示一支给定股票第i
天的价格。 - 你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润
- 返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回
0
- 输入:[7,1,5,3,6,4]
- 输出:5
public class L121 {
//贪心方法
public int maxProfit2(int[] prices) {
int minprice = Integer.MAX_VALUE;
int maxprofit = 0;
for (int i = 0; i < prices.length; i++) {
if (prices[i] < minprice) {
minprice = prices[i];
} else if (prices[i] - minprice > maxprofit) {
maxprofit = prices[i] - minprice;
}
}
return maxprofit;
}
//dp方法
public int maxProfit(int[] prices) {
int[][] dp=new int[prices.length][2];
dp[0][0]=0;//i天没有股票
dp[0][1]=-prices[0];//i天有股票
for(int i=1;i<prices.length;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],-prices[i]);
}
return dp[prices.length-1][0];
}
@Test
public void test1(){
int[] prices=new int[]{7,1,5,3,6,4};
maxProfit(prices);
}
}
Leetcode122-买卖股票的最佳时机2
- 给你一个整数数组 prices ,其中 prices[i] 表示某支股票第 i 天的价格。
- 在每一天,你可以决定是否购买和/或出售股票。你在任何时候 最多 只能持有 一股 股票。你也可以先购买,然后在 同一天 出售。
- 返回 你能获得的 最大 利润 。
- 输入:prices = [7,1,5,3,6,4]
- 输出:7
public class L122 {
//贪心
public int maxProfit(int[] prices) {
int sum=0;
int preprice=prices[0];
for(int i=0;i<prices.length;i++){
int nowprice=prices[i];
int cha=nowprice-preprice;
if(cha<0){
preprice=nowprice;
}else{
sum+=cha;
preprice=nowprice;
}
}
return sum;
}
//dp
public int maxProfit2(int[] prices) {
int[][] dp=new int[prices.length][2];
dp[0][0]=0;
dp[0][1]=-prices[0];//i天有股票
for(int i=1;i<prices.length;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[prices.length-1][0];
}
}
Leetcode714-买卖股票的最佳时机含手续费
- 给定一个整数数组 prices,其中 prices[i]表示第 i 天的股票价格 ;整数 fee 代表了交易股票的手续费用。
- 你可以无限次地完成交易,但是你每笔交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。
- 返回获得利润的最大值。
- 注意:这里的一笔交易指买入持有并卖出股票的整个过程,每笔交易你只需要为支付一次手续费。
- 输入:prices = [1, 3, 2, 8, 4, 9], fee = 2
- 输出:8
public int maxProfit(int[] prices, int fee) {
if (prices.length == 1) {
return 0;
}
int[][] dp = new int[prices.length][2];
dp[0][0] = 0;
dp[0][1] = -prices[0];
for (int i = 1; i < prices.length; i++) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i] - fee);
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
}
return dp[prices.length - 1][0];
}
Leetcode123-买卖股票的最佳时机3
- 给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。
- 设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。
- 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
- 输入:prices = [3,3,5,0,0,3,1,4]
- 输出:6
public int maxProfit(int[] prices) {
int len = prices.length;
// 边界判断, 题目中 length >= 1, 所以可省去
if (prices.length == 0) return 0;
/*
* 定义 5 种状态:
* 0: 没有操作, 1: 第一次买入, 2: 第一次卖出, 3: 第二次买入, 4: 第二次卖出
*/
int[][] dp = new int[len][5];
dp[0][1] = -prices[0];
// 初始化第二次买入的状态是确保 最后结果是最多两次买卖的最大利润
dp[0][3] = -prices[0];
for (int i = 1; i < len; i++) {
dp[i][1] = Math.max(dp[i - 1][1], -prices[i]);
dp[i][2] = Math.max(dp[i - 1][2], dp[i][1] + prices[i]);
dp[i][3] = Math.max(dp[i - 1][3], dp[i][2] - prices[i]);
dp[i][4] = Math.max(dp[i - 1][4], dp[i][3] + prices[i]);
}
return dp[len - 1][4];
}
Leetcode309-最佳买卖股票实际含冷冻期
- 给定一个整数数组prices,其中第 prices[i] 表示第 i 天的股票价格 。
- 设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):
- 卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天)。
- 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
- 输入: prices = [1,2,3,0,2]
- 输出: 3
public int maxProfit(int[] prices) {
if (prices.length == 0) {
return 0;
}
int n = prices.length;
// dp[i][0]: 手上持有股票的最大收益
// dp[i][1]: 手上不持有股票,并且处于冷冻期中的累计最大收益
// dp[i][2]: 手上不持有股票,并且不在冷冻期中的累计最大收益
int[][] dp = new int[n][3];
dp[0][0] = -prices[0];
for (int i = 1; i < n; ++i) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][2] - prices[i]);
dp[i][1] = dp[i - 1][0] + prices[i];
dp[i][2] = Math.max(dp[i - 1][1], dp[i - 1][2]);
}
return Math.max(dp[n - 1][1], dp[n - 1][2]);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏