文章目录
毁灭吧...
leetcode.121. 买卖股票的最佳时机
题目描述
给定一个数组 prices
,它的第 i
个元素 prices[i]
表示一支给定股票第 i
天的价格。
你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。
返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0
。
示例 1:
输入:[7,1,5,3,6,4]
输出:5
解释:在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。
示例 2:
输入:prices = [7,6,4,3,1]
输出:0
解释:在这种情况下, 没有交易完成, 所以最大利润为 0。
提示:
1 <= prices.length <= 105
0 <= prices[i] <= 104
coding
class Solution {
public int maxProfit(int[] prices) {
int len = prices.length;
if (len < 1) {
return 0;
}
// min : 记录之前遍历到的最小股票值作为购买的花费
int min = prices[0];
// res : 记录前 i 天的最优解
int res = 0;
for (int i = 1; i < len; i ++) {
min = Math.min(min, prices[i]);
res = Math.max(res, prices[i] - min);
}
return res;
}
}
leetcode.122. 买卖股票的最佳时机 II
题目描述
给你一个整数数组 prices
,其中 prices[i]
表示某支股票第 i
天的价格。
在每一天,你可以决定是否购买和/或出售股票。你在任何时候 最多 只能持有 一股 股票。你也可以先购买,然后在 同一天 出售。
返回 你能获得的 最大 利润 。
示例 1:
输入:prices = [7,1,5,3,6,4]
输出:7
解释:在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5 - 1 = 4 。
随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6 - 3 = 3 。
总利润为 4 + 3 = 7 。
示例 2:
输入:prices = [1,2,3,4,5]
输出:4
解释:在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5 - 1 = 4 。
总利润为 4 。
示例 3:
输入:prices = [7,6,4,3,1]
输出:0
解释:在这种情况下, 交易无法获得正利润,所以不参与交易可以获得最大利润,最大利润为 0 。
提示:
1 <= prices.length <= 3 * 104
0 <= prices[i] <= 104
coding
class Solution {
// dp 递推获取最优的股票获利
public int maxProfit(int[] prices) {
int len = prices.length;
int[][] dp = new int[len][2];
dp[0][0] = 0;
dp[0][1] = -prices[0];
for (int i = 1; i < len; i ++) {
// 第 i 天没股票的收益:
// 1. 昨天卖了
// 2. 昨天就没得
dp[i][0] = Math.max(dp[i - 1][1] + prices[i], dp[i - 1][0]);
// 第 i 天有股票的收益:
// 1. 昨天刚买
// 2. 昨天就有
dp[i][1] = Math.max(dp[i - 1][0] - prices[i], dp[i - 1][1]);
}
return dp[len - 1][0];
}
// 0 -> 2 天的 收益表示
// 1. prices[2] - prices[0];
// 2. prices[2] - prices[1] + prices[1] - prices[0]
// ||
// (prices[2] - prices[1]) + (prices[1] - prices[0])
//
// => 为了拿最多收益, 故只需要计算后一天比前一天价值多出去的部分即可
public int maxProfit(int[] prices) {
int len = prices.length;
int res = 0;
for (int i = 1; i < len; i ++) {
if (prices[i] > prices[i - 1]) {
res += prices[i] - prices[i - 1];
}
}
return res;
}
}
leetcode.123. 买卖股票的最佳时机 III
题目描述
给定一个数组,它的第 i
个元素是一支给定的股票在第 i
天的价格。
设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。
注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
示例 1:
输入:prices = [3,3,5,0,0,3,1,4]
输出:6
解释:在第 4 天(股票价格 = 0)的时候买入,在第 6 天(股票价格 = 3)的时候卖出,这笔交易所能获得利润 = 3-0 = 3 。
随后,在第 7 天(股票价格 = 1)的时候买入,在第 8 天 (股票价格 = 4)的时候卖出,这笔交易所能获得利润 = 4-1 = 3 。
示例 2:
输入:prices = [1,2,3,4,5]
输出:4
解释:在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。
因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。
示例 3:
输入:prices = [7,6,4,3,1]
输出:0
解释:在这个情况下, 没有交易完成, 所以最大利润为 0。
示例 4:
输入:prices = [1]
输出:0
提示:
1 <= prices.length <= 105
0 <= prices[i] <= 105
coding
class Solution {
public int maxProfit(int[] prices) {
int len = prices.length;
int[][][] dp = new int[len][2][3];
// dp[i][cnt][k]
// i -> 第 i 天
// cnt -> 当前拥有票数 0 or 1
// k -> 购买的总次数
// 初始化状态
dp[0][0][0] = 0;
dp[0][0][1] = 0;
dp[0][1][1] = -prices[0];
dp[0][1][2] = -prices[0];
for (int i = 1; i < len; i ++) {
// 票数 = 0 :
// 1. 昨天就是这样
// 2. 昨天刚卖了
dp[i][0][1] = Math.max(dp[i - 1][0][1], dp[i - 1][1][1] + prices[i]);
dp[i][0][2] = Math.max(dp[i - 1][0][2], dp[i - 1][1][2] + prices[i]);
// 票数 = 1 :
// 1. 昨天就是这样
// 2. 昨天刚买的
dp[i][1][1] = Math.max(dp[i - 1][1][1], dp[i - 1][0][0] - prices[i]);
dp[i][1][2] = Math.max(dp[i - 1][1][2], dp[i - 1][0][1] - prices[i]);
}
return dp[len - 1][0][2];
}
}
leetcode.188. 买卖股票的最佳时机 IV
题目描述
给定一个整数数组 prices
,它的第 i
个元素 prices[i]
是一支给定的股票在第 i
天的价格。
设计一个算法来计算你所能获取的最大利润。你最多可以完成 k 笔交易。
注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
示例 1:
输入:k = 2, prices = [2,4,1]
输出:2
解释:在第 1 天 (股票价格 = 2) 的时候买入,在第 2 天 (股票价格 = 4) 的时候卖出,这笔交易所能获得利润 = 4-2 = 2 。
示例 2:
输入:k = 2, prices = [3,2,6,5,0,3]
输出:7
解释:在第 2 天 (股票价格 = 2) 的时候买入,在第 3 天 (股票价格 = 6) 的时候卖出, 这笔交易所能获得利润 = 6-2 = 4 。
随后,在第 5 天 (股票价格 = 0) 的时候买入,在第 6 天 (股票价格 = 3) 的时候卖出, 这笔交易所能获得利润 = 3-0 = 3 。
提示:
0 <= k <= 100
0 <= prices.length <= 1000
0 <= prices[i] <= 1000
coding
class Solution {
public int maxProfit(int k, int[] prices) {
int len = prices.length;
if (len < 2 || k == 0) {
return 0;
}
// dp[i][cnt][k]
// i -> 第 i 天
// cnt -> 当前拥有票数 0 or 1
// k -> 购买的总次数
// 初始化状态
int[][][] dp = new int[len][2][k + 1];
// 票数 cnt = 1 -> 净购买票数为 1, 默认是花费了第一天的票价买回来的
for (int i = 1; i <= k; i ++) {
dp[0][1][i] = -prices[0];
}
for (int i = 1; i < len; i ++) {
for (int j = 1; j <= k; j ++) {
// 同 leetcode.123. 买卖股票的最佳时机 III
dp[i][0][j] = Math.max(dp[i - 1][0][j], dp[i - 1][1][j] + prices[i]);
dp[i][1][j] = Math.max(dp[i - 1][1][j], dp[i - 1][0][j - 1] - prices[i]);
}
}
return dp[len - 1][0][k];
}
}
leetcode.309. 最佳买卖股票时机含冷冻期
题目描述
给定一个整数数组prices
,其中第 prices[i]
表示第 i
天的股票价格 。
设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):
- 卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天)。
注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
示例 1:
输入: prices = [1,2,3,0,2]
输出: 3
解释: 对应的交易状态为: [买入, 卖出, 冷冻期, 买入, 卖出]
示例 2:
输入: prices = [1]
输出: 0
提示:
1 <= prices.length <= 5000
0 <= prices[i] <= 1000
coding
class Solution {
public int maxProfit(int[] prices) {
int len = prices.length;
int[][][] dp = new int[len][2][2];
// dp[i][cnt][isOk]
// i -> 第 i 天
// cnt -> 当前拥有票数 0 or 1
// isOk -> 当前状态是否可进行购票
// 第一天自己作的, 买了又退, 导致冻结快乐?hhhh
dp[0][0][0] = 0;
// 第一天啥也没干
dp[0][0][1] = 0;
// 第一天买票啦
dp[0][1][0] = -prices[0];
for (int i = 1; i < len; i ++) {
// 无票且不能购买(冻结啦): 昨天刚卖
dp[i][0][0] = dp[i - 1][1][0] + prices[i];
// 当前没票, 能够买:
// 1. 昨天就没得票, 且可购买
// 2. 前天卖票了, 昨天冷冻期
dp[i][0][1] = Math.max(dp[i - 1][0][1], dp[i - 1][0][0]);
// 有票啦: 昨天刚买 or 之前就有
dp[i][1][0] = Math.max(dp[i - 1][1][0], dp[i - 1][0][1] - prices[i]);
// System.out.println(dp[i][0][0] + "\t" + dp[i][1][0] + "\t" + dp[i][0][1]);
}
return Math.max(dp[len - 1][0][1], dp[len - 1][0][0]);
}
}
leetcode.714. 买卖股票的最佳时机含手续费
题目描述
给定一个整数数组 prices
,其中 prices[i]
表示第 i
天的股票价格 ;整数 fee
代表了交易股票的手续费用。
你可以无限次地完成交易,但是你每笔交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。
返回获得利润的最大值。
注意:这里的一笔交易指买入持有并卖出股票的整个过程,每笔交易你只需要为支付一次手续费。
示例 1:
输入:prices = [1, 3, 2, 8, 4, 9], fee = 2
输出:8
解释:能够达到的最大利润:
在此处买入 prices[0] = 1
在此处卖出 prices[3] = 8
在此处买入 prices[4] = 4
在此处卖出 prices[5] = 9
总利润: ((8 - 1) - 2) + ((9 - 4) - 2) = 8
示例 2:
输入:prices = [1,3,7,5,10,3], fee = 3
输出:6
提示:
1 <= prices.length <= 5 * 104
1 <= prices[i] < 5 * 104
0 <= fee < 5 * 104
coding
class Solution {
// 只需在之前的情况上, 买票的时候多减去需要的手续费即可
public int maxProfit(int[] prices, int fee) {
int len = prices.length;
int[][] dp = new int[len][2];
dp[0][0] = 0;
dp[0][1] = -prices[0] - fee;
for (int i = 1; i < len; i ++) {
dp[i][0] = Math.max(dp[i - 1][1] + prices[i], dp[i - 1][0]);
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i] - fee);
}
return dp[len - 1][0];
}
}