aboutsummaryrefslogtreecommitdiff
path: root/Stock.java
blob: 7a7915b560236e9282d260fa95e80b08a465aef8 (plain)
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
import java.lang.*;
import java.util.*;

class Stock {
	public static int maxProfit(int[] prices) {
		int s = 0;
		int e = prices.length - 1;
		int low = Integer.MAX_VALUE;
		int high = 0;
		while (s < e) {
			if (prices[s] < low)
				low = prices[s];
			if (prices[e] > high)
				high = prices[e];
			s++;
			e--;
		}
		return high - low > 0 ? high - low : 0;
	}

	public static void main(String[] args) {
		int[] prices = {7, 1, 5, 3, 6, 4};
		System.out.println(maxProfit(prices));
	}
}