Skip to content

Instantly share code, notes, and snippets.

@thmain
Created August 20, 2017 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/6642850524724892302a003c52570101 to your computer and use it in GitHub Desktop.
Save thmain/6642850524724892302a003c52570101 to your computer and use it in GitHub Desktop.
public class StockSingleSellDandC {
public Result maxProfit(int [] prices, int start, int end){
if(start>=end){
return new Result(0,0,0);
}
int mid = start + (end-start)/2;
Result leftResult = maxProfit(prices,start,mid);
Result rightResult = maxProfit(prices,mid+1,end);
int minLeftIndex = getMinIndex(prices, start, mid);
int maxRightIndex = getMaxIndex(prices, mid, end);
int centerProfit = prices[maxRightIndex] - prices[minLeftIndex];
if(centerProfit>leftResult.profit && centerProfit>rightResult.profit){
return new Result(centerProfit,minLeftIndex,maxRightIndex);
}else if(leftResult.profit>centerProfit && rightResult.profit>centerProfit){
return leftResult;
}else{
return rightResult;
}
}
public int getMinIndex(int [] A, int i, int j){
int min = i;
for (int k = i+1; k <=j ; k++) {
if(A[k]<A[min])
min = k;
}
return min;
}
public int getMaxIndex(int [] A, int i, int j){
int max = i;
for (int k = i+1; k <=j ; k++) {
if(A[k]>A[max])
max = k;
}
return max;
}
public static void main(String[] args) {
StockSingleSellDandC m = new StockSingleSellDandC();
int [] prices = { 200, 500, 1000, 700, 30, 400, 900, 400, 50};
int start = 0;
int end = prices.length-1;
Result result = m.maxProfit(prices,start,end);
System.out.println("Maximum Profit(Divide & Conquer): " +result.profit + ", buy date index: " + result.buyDateIndex +
", sell date index: " + result.sellDateIndex);
}
}
class Result{
int profit=0;
int buyDateIndex=0;
int sellDateIndex=0;
public Result(int profit, int buyDateIndex, int sellDateIndex){
this.profit = profit;
this.buyDateIndex = buyDateIndex;
this.sellDateIndex = sellDateIndex;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment