diff options
author | Michael Hunteman <michael@michaelted.xyz> | 2022-09-22 10:14:04 -0500 |
---|---|---|
committer | Michael Hunteman <michael@michaelted.xyz> | 2022-09-22 10:14:04 -0500 |
commit | 6522012065712fb0ece31bff9ff10b38a83b10e1 (patch) | |
tree | 743f3c13d08be9f60bad57b74e7a1fe7cc675e89 /Stock.java |
Initial commit
Diffstat (limited to 'Stock.java')
-rw-r--r-- | Stock.java | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/Stock.java b/Stock.java new file mode 100644 index 0000000..7a7915b --- /dev/null +++ b/Stock.java @@ -0,0 +1,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)); + } +} |