-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBuy_Sell.java
More file actions
66 lines (53 loc) · 1.69 KB
/
Buy_Sell.java
File metadata and controls
66 lines (53 loc) · 1.69 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*public class Buy_Sell {
public static void main(String[] args) {
int[] prices = { 7, 1, 5, 3, 6, 4 };
int buyprice = prices[0];
int i = 0;
int buyindexno = 0;
while (i < prices.length) {
if (prices[i] < buyprice) {
buyprice = prices[i];
buyindexno = i;
}
i++;
}
int j = buyindexno + 1;
int sellprice = buyprice;
int sellindexno = 0;
if (buyindexno < prices.length - 1) {
while (j < prices.length) {
if (prices[j] > sellprice) {
sellprice = prices[j];
sellindexno = j;
}
j++;
}
} else {
System.out.println(0);
}
System.out.println(sellprice - buyprice);
}
}
*/
public class MaxProfit {
public static int maxProfit(int[] prices) {
if (prices == null || prices.length < 2) {
return 0;
}
int minPrice = prices[0];
int maxProfit = 0;
for (int i = 1; i < prices.length; i++) {
// Update the minimum price if a lower price is encountered
minPrice = Math.min(minPrice, prices[i]);
// Update the maximum profit if a better selling opportunity is found
maxProfit = Math.max(maxProfit, prices[i] - minPrice);
}
return maxProfit;
}
public static void main(String[] args) {
int[] prices1 = { 7, 1, 5, 3, 6, 4 };
System.out.println(maxProfit(prices1)); // Output: 5
int[] prices2 = { 7, 6, 4, 3, 1 };
System.out.println(maxProfit(prices2)); // Output: 0
}
}