-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.java
More file actions
64 lines (56 loc) · 1.78 KB
/
test.java
File metadata and controls
64 lines (56 loc) · 1.78 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
// TO BE IGNORED!!!
// public class test {
// public static void main(String[] args) {
// int high, low;
// int nums[] = { 7, 1, 5, 3, 6, 4 };
// high = 0;
// low = 0;
// for (int i = 0; i < nums.length; i++) {
// System.out.println(i + " -> " + nums[i]);
// if (nums[i] < nums[low]) {
// low = i;
// System.out.println(low + " (low) -> " + nums[low]);
// }
// if (nums[i] > nums[high]) {
// high = i;
// System.out.println(high + " (high) -> " + nums[high]);
// }
// }
// System.out.println(high + " (high) -> " + nums[high]);
// System.out.println(low + " (low) -> " + nums[low]);
// }
// }
public class test {
public static void profit(int array[]) {
int buyPrice = Integer.MAX_VALUE;
int maxProfit = 0;
for (int i = 0; i < array.length; i++) {
if (buyPrice < array[i]) {
int profit = array[i] - buyPrice;
maxProfit = Math.max(maxProfit, profit);
} else {
buyPrice = array[i];
}
}
System.out.println("the max profit is " + maxProfit);
}
public static void maxMin(int array[]) {
int low = 0;
int high = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] < array[low]) {
low = i;
}
if (array[i] > array[high]) {
high = i;
}
}
System.out.println(array[low]);
System.out.println(array[high]);
}
public static void main(String[] args) {
int numbers[] = { 7, 1, 5, 3, 6, 4 };
profit(numbers);
maxMin(numbers);
}
}