121.买卖股票的最佳时机
class Solution {
public int maxProfit ( int [ ] prices) {
int minPrice = prices[ 0 ] ;
int maxProfit = 0 ;
for ( int i = 1 ; i < prices. length; i++ ) {
if ( prices[ i] < minPrice)
minPrice = prices[ i] ;
else {
maxProfit = Math . max ( maxProfit, prices[ i] - minPrice) ;
}
}
return maxProfit;
}
}
122.买卖股票的最佳时机Ⅱ
1.动态规划
class Solution {
public int maxProfit ( int [ ] prices) {
int n = prices. length;
int dp0 = 0 ;
int dp1 = - prices[ 0 ] ;
for ( int i = 1 ; i < n; i++ ) {
int newDp0 = Math . max ( dp0, dp1+ prices[ i] ) ;
int newDp1 = Math . max ( dp0- prices[ i] , dp1) ;
dp0 = newDp0;
dp1 = newDp1;
}
return dp0;
}
}
2.贪心
class Solution {
public int maxProfit ( int [ ] prices) {
int ans = 0 ;
for ( int i = 1 ; i < prices. length; i++ ) {
ans += Math . max ( 0 , prices[ i] - prices[ i- 1 ] ) ;
}
return ans;
}
}
123.买卖股票的最佳时机Ⅲ
动态规划!
class Solution {
public int maxProfit ( int [ ] prices) {
int n = prices. length;
int buy1 = - prices[ 0 ] , sel1 = 0 ;
int buy2 = - prices[ 0 ] , sel2 = 0 ;
for ( int i = 1 ; i < n; i++ ) {
int newBuy1 = Math . max ( buy1, - prices[ i] ) ;
int newSel1 = Math . max ( sel1, buy1+ prices[ i] ) ;
int newBuy2 = Math . max ( buy2, sel1- prices[ i] ) ;
int newSel2 = Math . max ( sel2, buy2+ prices[ i] ) ;
buy1 = newBuy1; sel1 = newSel1;
buy2 = newBuy2; sel2 = newSel2;
}
return Math . max ( sel1, sel2) ;
}
}
188.买卖股票的最佳时机Ⅳ
class Solution {
public int maxProfit ( int k, int [ ] prices) {
int n = prices. length, ans = 0 ;
if ( n == 0 ) return 0 ;
int [ ] [ ] dp = new int [ n] [ k* 2 ] ;
for ( int j = 0 ; j < k; j++ ) {
dp[ 0 ] [ 2 * j] = - prices[ 0 ] ;
dp[ 0 ] [ 2 * j+ 1 ] = 0 ;
}
for ( int i = 1 ; i < n; i++ ) {
for ( int j = 0 ; j < k; j++ ) {
if ( j == 0 ) {
dp[ i] [ 0 ] = Math . max ( dp[ i- 1 ] [ 0 ] , - prices[ i] ) ;
dp[ i] [ 1 ] = Math . max ( dp[ i- 1 ] [ 1 ] , dp[ i- 1 ] [ 0 ] + prices[ i] ) ;
}
else {
dp[ i] [ 2 * j] = Math . max ( dp[ i- 1 ] [ 2 * j] , dp[ i- 1 ] [ 2 * j- 1 ] - prices[ i] ) ;
dp[ i] [ 2 * j+ 1 ] = Math . max ( dp[ i- 1 ] [ 2 * j+ 1 ] , dp[ i- 1 ] [ 2 * j] + prices[ i] ) ;
}
if ( i == n- 1 ) {
ans = Math . max ( ans, dp[ i] [ 2 * j+ 1 ] ) ;
}
}
}
return ans;
}
}
309.买卖股票的最佳时机含冷冻期
class Solution {
public int maxProfit ( int [ ] prices) {
int n = prices. length;
int [ ] [ ] dp = new int [ n] [ 3 ] ;
dp[ 0 ] [ 0 ] = 0 ;
dp[ 0 ] [ 1 ] = - prices[ 0 ] ;
dp[ 0 ] [ 2 ] = 0 ;
for ( int i = 1 ; i < n; i++ ) {
dp[ i] [ 0 ] = Math . max ( dp[ i- 1 ] [ 0 ] , dp[ i- 1 ] [ 2 ] ) ;
dp[ i] [ 1 ] = Math . max ( dp[ i- 1 ] [ 1 ] , dp[ i- 1 ] [ 0 ] - prices[ i] ) ;
dp[ i] [ 2 ] = dp[ i- 1 ] [ 1 ] + prices[ i] ;
}
return Math . max ( dp[ n- 1 ] [ 0 ] , dp[ n- 1 ] [ 2 ] ) ;
}
}
714.买卖股票的最佳时机含手续费
1.动态规划
class Solution {
public int maxProfit ( int [ ] prices, int fee) {
int n = prices. length;
int [ ] [ ] dp = new int [ n] [ 2 ] ;
dp[ 0 ] [ 0 ] = 0 ;
dp[ 0 ] [ 1 ] = - prices[ 0 ] - fee;
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] - fee) ;
}
return dp[ n- 1 ] [ 0 ] ;
}
}
2.贪心
class Solution {
public int maxProfit ( int [ ] prices, int fee) {
int n = prices. length;
int buy = prices[ 0 ] + fee, ans = 0 ;
for ( int i = 1 ; i < n; i++ ) {
if ( prices[ i] + fee < buy) {
buy = prices[ i] + fee;
}
else if ( prices[ i] > buy) {
ans += prices[ i] - buy;
buy = prices[ i] ;
}
}
return ans;
}
}