Skip to content

Instantly share code, notes, and snippets.

@rhn89
Created June 21, 2017 04:53
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 rhn89/7cca9e73d8fa3a1fe7b6968216ec38ac to your computer and use it in GitHub Desktop.
Save rhn89/7cca9e73d8fa3a1fe7b6968216ec38ac to your computer and use it in GitHub Desktop.
/**
* Created by rohansingh on 6/20/17.
*/
public class MaxProfit{
public static void main(String[] args) {
/**
*The following array shows the price of a stock on the i-th day
* and serves an input to the maxProfit Method
*/
int arr[] = {10,9,11,17,21,9,10,30235,3556,35311};
System.out.printf("Max Profit: %d\n", maxProfit(arr));
}
public static int maxProfit(int[] prices) {
int profit = 0;
for(int i=0;i<prices.length-1;i++){
if(isPriceIncreasing(prices[i],prices[i+1]))
profit += (prices[i+1] - prices[i]);
}
return profit;
}
public static boolean isPriceIncreasing(int a, int b){
if(b>a) return true;
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment