-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBestTimetoBuyandSellStock.java
More file actions
32 lines (28 loc) · 968 Bytes
/
BestTimetoBuyandSellStock.java
File metadata and controls
32 lines (28 loc) · 968 Bytes
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
26
27
28
29
30
31
32
/*
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
*/
import java.util.*;
public class BestTimetoBuyandSellStock {
public static int maxProfit(int[] prices) {
int res = 0;
if(prices.length <= 0) return 0;
int m = Integer.MAX_VALUE;
for(int i = 0; i < prices.length; i++){
m = Math.min(prices[i], m);
res = Math.max(prices[i] - m, res);
}
return res;
}
public static void main(String[] args) {
int[] nums1 = new int[] {1, 2, 3, 0, 2, 7};
System.out.println("Array: ");
for(int i=0; i < nums1.length; i++){
System.out.print(nums1[i] + " ");
}
System.out.println();
int res = maxProfit(nums1);
System.out.println("Maximum profit: " + res);
return;
}
}