Created
June 21, 2017 04:53
-
-
Save rhn89/7cca9e73d8fa3a1fe7b6968216ec38ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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