-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuySellStock.java
More file actions
31 lines (27 loc) · 844 Bytes
/
buySellStock.java
File metadata and controls
31 lines (27 loc) · 844 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
public class buySellStock {
public static int maxProfit(int[] prices) {
int buy = 0;
int sell = 0;
for(int i=0; i<prices.length; i++){
for(int j=i+1; j<prices.length; j++){
if(prices[i] < prices[j]){
buy = prices[i];
return buy;
}
for(int k=buy+1; k<prices.length; k++){
if(prices[k] > prices[k+1]){
sell = prices[k];
return sell;
}
}
}
}
int profit = sell-buy;
System.out.print(profit);
return profit;
}
public static void main(String[] args){
int[] prices = {7,1,5,3,6,4};
System.out.print(maxProfit(prices));
}
}