Best Time to Buy and Sell Stock, Best Time to Buy and Sell Stock II, Best Time to Buy and Sell Stock III

Best Time to Buy and Sell Stock

Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Brute Force Solution

1
2
3
4
5
6
7
8
9
public int maxProfit(int[] prices) {
int maxp=0, low=Integer.MAX_VALUE;
for(int i=0;i<prices.length;i++){
if(prices[i]<low) low=prices[i];
if(prices[i]-low>maxp)
maxp=prices[i]-low;
}
return maxp;
}

DP Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public int maxProfit(int[] prices) {
int maxp=0, tmp=0;
if (prices == null || prices.length <2) { return 0;}
int[] t=new int[prices.length-1];
for(int i=0;i<prices.length-1;i++){
t[i]=prices[i+1]-prices[i];
}
for(int i=0;i<t.length;i++){
tmp+=t[i];
if(tmp>maxp) maxp=tmp;
if(tmp<0) tmp=0;
}
return maxp;
}

Best Time to Buy and Sell Stock II

Greedy Solution: trasaction once there exist profit.

1
2
3
4
5
6
7
8
9
public int maxProfit(int[] prices) {
if(prices.length == 0) return 0;
int ans = 0;
for(int i=1; i<prices.length; i++){
if(prices[i] > prices[i-1])
ans += prices[i]-prices[i-1];
}
return ans;
}

DP Solution

1
2
3
4
5
6
7
8
9
10
11
12
public int maxProfit(int[] prices) {
int maxp=0, tmp=0,curmax=0;
if (prices == null || prices.length <2) { return 0;}
int[] t=new int[prices.length-1];
for(int i=0;i<prices.length-1;i++){
t[i]=prices[i+1]-prices[i];
}
for(int i=0;i<t.length;i++){
if(t[i]>0) maxp+=t[i];
}
return maxp;
}

Best Time to Buy and Sell Stock III

Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most two transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

DP Solution

First thought: we can cut the array in half in n ways, and get the max profit from both sides using solution in the first question. This will be O(n^2), too LTE…
Using DP to store the max profit from both sides? just scan twice from both directions and update the max profit array for [0…i] and [i…n-1]. Return the max value when a[i]+b[i] is max.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class Solution {
public int maxProfit(int[] prices) {
if(prices==null||prices.length<2) return 0;
int n=prices.length, maxsofar=0, low=0,high=0;
//curmax[i] shows max profit from [0..i]
int[] curmax=new int[n];
curmax[0]=0;low=prices[0];
for(int i=1;i<n;i++){
if(prices[i]<low) low=prices[i];
if(prices[i]-low>maxsofar) maxsofar=prices[i]-low;
curmax[i]=maxsofar;
}
//backmax[i] shows max profits from [i...n-1]
int[] backmax=new int[n];maxsofar=0;
backmax[n-1]=0; high=prices[n-1];
for(i=n-2;i>=0;i--){
if(high<prices[i]) high=prices[i];
if(high-prices[i]>maxsofar) maxsofar=high-prices[i];
backmax[i]=maxsofar;
}
maxsofar=0;
for(i=0;i<n;i++){
if(backmax[i]+curmax[i]>maxsofar) maxsofar = backmax[i]+curmax[i];
}
return maxsofar;
}
}

General DP Solution

Two cases select max as f[k][i]

  • transact on i: p[i]-p[j]+f[k-1][j] or write as p[i]-max(f[k-1][j]-p[j]) j belongs to [0,i-1]

  • no transaction on i: t[k][i-1]
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    public int maxProfit(int[] prices) {
    if(prices==null||prices.length<2) return 0;
    int n=prices.length, maxsofar=0, K=2;
    int[][] f=new int[K+1][n];
    for (int kk = 1; kk <= K; kk++) {
    int tmpMax = f[kk-1][0] - prices[0];
    for (int ii = 1; ii < n; ii++) {
    f[kk][ii] = Math.max(f[kk][ii-1], prices[ii] + tmpMax);
    tmpMax = Math.max(tmpMax, f[kk-1][ii] - prices[ii]);
    maxsofar = Math.max(f[kk][ii], maxsofar);
    }
    }
    return maxsofar;
    }