-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathARRAY12.java
More file actions
41 lines (37 loc) · 1.1 KB
/
ARRAY12.java
File metadata and controls
41 lines (37 loc) · 1.1 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
public class ARRAY12 {
public static int MaxProfit(int cost[]) {
int maxProfit = 0;
for (int cp = 1; cp <= cost.length; cp++) {
for (int sp = cp; sp <= cost.length; sp++) {
int profit = (sp - cp);
if (profit > maxProfit) {
maxProfit = profit;
}
}
}
if (maxProfit > 0) {
return maxProfit;
} else {
return 0;
}
}
//METHOD 2
public static int BuyAndSell(int price[]) {
int buyprice = Integer.MAX_VALUE;
int maxprofit = 0;
for (int i = 0; i < price.length; i++) {
if (buyprice < price[i]) {
int profit = price[i] - buyprice;
maxprofit = Math.max(profit, maxprofit);
} else {
buyprice = price[i];
}
}
return maxprofit;
}
public static void main(String[] args) {
// STOCK MARKET PROFIT!
int cost[] = {7, 1, 5, 3, 6, 4};
System.out.println("MAXIMUM PROFIT IS: " + MaxProfit(cost));
}
}