aboutsummaryrefslogtreecommitdiff
path: root/Stock.java
diff options
context:
space:
mode:
Diffstat (limited to 'Stock.java')
-rw-r--r--Stock.java25
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));
+ }
+}