Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // https://www.interviewcake.com/question/java/stock-price
- public class Main{
- public static void main(String[] args) {
- int[] prices = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
- System.out.println(getMaxProfit(prices));
- }
- public static int getMaxProfit(int[] stockPrices){
- int maxProfit = 0;
- if(stockPrices.length <= 1){
- throw new IllegalArgumentException();
- }
- int min = Integer.MAX_VALUE;
- int max = Integer.MIN_VALUE;
- int i = 0;
- int j = stockPrices.length - 1;
- while(i < j){
- if(stockPrices[i] < min){
- min = stockPrices[i];
- }
- if(stockPrices[j] > max){
- max = stockPrices[j];
- }
- i++;
- j--;
- }
- maxProfit = max - min;
- return maxProfit;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement