1. 买卖股票的最佳时机
leetcode-cn.com/problems/be…
class Solution {
public int maxProfit(int[] prices) {
int n = prices.length;
if(n <= 1) return 0;
int min = prices[0] , max = 0;
for(int num : prices){
max = Math.max(max , num - min);
min = Math.min(min , num);
}
return max;
}
}
2. 买卖股票的最佳时机2-尽可能多的交易
leetcode-cn.com/problems/be…
class Solution {
public int maxProfit(int[] prices) {
int n = prices.length;
if(n <= 1) return 0;
int res = 0;
for(int i = 1 ; i < n ;i++){
res += Math.max(prices[i] - prices[i-1] , 0);
}
return res;
}
}
3. 买卖股票的最佳时机3-最多两次交易
leetcode-cn.com/problems/be…
class Solution {
public int maxProfit(int[] prices) {
int n = prices.length;
if(n < 2) return 0;
int[][][] dp = new int[n][3][2];
for(int i = 0 ; i < n ; i++){
for(int j = 0 ;j <= 2;j++){
if(i == 0){
dp[i][j][0] = 0;
dp[i][j][1] = -prices[0];
continue;
}
if(j == 0){
dp[i][j][1] = Integer.MIN_VALUE;
dp[i][j][0] = 0;
continue;
}
dp[i][j][1] = Math.max(dp[i-1][j][1] , dp[i-1][j-1][0]-prices[i]);
dp[i][j][0] = Math.max(dp[i-1][j][0] , dp[i-1][j][1]+prices[i]);
}
}
return dp[n-1][2][0];
}
}
4. 买卖股票的最佳时机4-最多k次交易
leetcode-cn.com/problems/be…
class Solution {
public int maxProfit(int k, int[] prices) {
int n = prices.length;
if(n < 2) return 0;
if(k > n/2){
int res = 0;
for(int i = 1; i < n ;i++){
res += Math.max(0 , prices[i] - prices[i-1]);
}
return res;
}
int[][][] dp = new int[n][k+1][2];
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j <= k ; j++){
if(i == 0){
dp[i][j][0] = 0;
dp[i][j][1] = -prices[0];
continue;
}
if(j == 0){
dp[i][j][0] = 0;
dp[i][j][1] = Integer.MIN_VALUE;
continue;
}
dp[i][j][1] = Math.max(dp[i-1][j][1] , dp[i-1][j-1][0]-prices[i]);
dp[i][j][0] = Math.max(dp[i-1][j][0] , dp[i-1][j][1] + prices[i]);
}
}
return dp[n-1][k][0];
}
}
5. 买卖股票的最佳时机5-含有冷冻期
leetcode-cn.com/problems/be…
class Solution {
public int maxProfit(int[] prices) {
int n = prices.length;
if (n < 1) return 0;
int[][] dp = new int[n][2];
for (int i = 0; i < n; i++) {
if (i == 0){
dp[0][0] = 0;
dp[0][1] = -prices[0];
continue;
}
if (i == 1){
dp[1][0] = Math.max(0 , prices[1] - prices[0]);
dp[1][1] = Math.max(-prices[0] , -prices[1]);
continue;
}
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-2][0] - prices[i]);
}
return dp[n-1][0];
}
}
6. 买卖股票的最佳时机6-含有手续费
leetcode-cn.com/problems/be…
class Solution {
public int maxProfit(int[] prices, int fee) {
int n = prices.length;
if (n < 1) return 0;
int[][] dp = new int[n][2];
for (int i = 0; i < n; i++) {
if (i == 0){
dp[0][0] = 0;
dp[0][1] = -prices[i] - fee;
continue;
}
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[n-1][0];
}
}