-
Notifications
You must be signed in to change notification settings - Fork 692
/
FindMaximumSellProfit.java
94 lines (57 loc) · 2.48 KB
/
FindMaximumSellProfit.java
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package arrays;
import com.sun.tools.javac.util.Pair;
public class FindMaximumSellProfit {
/*
* Given an array representing prices of the stock on different days,
* find the maximum profit that can be earned by performing maximum of one transaction.
* A transaction consists of activity of buying and selling the stock on different or same days.
*
* Eg: 1 - {100, 80, 120, 130, 70, 60, 100, 125}
* the price of the stock on day-1 is 100, on day-2 is 80 and so on.
* The maximum profit that could be earned in this window is 65 (buy at 60 and sell at 125).
*
* Eg: 2 - For price array - {100, 80, 70, 65, 60, 55, 50}, maximum profit that could be earned is 0.
*
* */
public static int findMaximumProfit(int[] stockPrices) {
int minimumPrice = Integer.MAX_VALUE;
int maxProfit = 0;
/*
* The profit is the maximum value of the maximumProfit so far and
* the difference between the current element(selling price) and the minimum buying price - i.e. profit for that buy
* Profit = current selling price - the minimum buying price
*
* Minimum buying price is the minimum value between the current element and the minimum price
* */
for(int i=0; i<stockPrices.length; i++) {
maxProfit = Math.max(maxProfit, stockPrices[i]-minimumPrice);
minimumPrice = Math.min(minimumPrice, stockPrices[i]);
}
return maxProfit;
}
public static Pair<Integer, Integer> findMaximumSellProfit(int length, int[] arr) {
int currentBuy = arr[0];
int globalSell = arr[1];
int globalProfit = globalSell - currentBuy;
int currentProfit;
for(int i=1; i< length; i++) {
int currentSell = arr[i];
currentProfit = currentSell - currentBuy;
if(currentProfit > globalProfit) {
globalProfit = currentProfit;
globalSell = arr[i];
}
if(currentBuy > arr[i]) {
currentBuy = arr[i];
}
}
int optimalBuyPrice = globalSell-globalProfit;
int optimalSellPrice = globalSell;
return Pair.of(optimalBuyPrice, optimalSellPrice) ;
}
public static void main(String[] args) {
int[] arr = {100, 80, 120, 130, 70, 60, 100, 125};
System.out.println(findMaximumSellProfit(arr.length, arr));
System.out.println(findMaximumProfit(arr));
}
}