- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo121.java
27 lines (25 loc) · 539 Bytes
/
No121.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
packageAlgorithm.leetcode.Array;
publicclassNo121 {
publicstaticvoidmain(String[] args) {
int[] a = { 7, 1, 5, 3, 6, 4 };
intb = maxProfit(a);
System.out.println(b);
}
publicstaticintmaxProfit(int[] prices) {
intlen = prices.length;
if (len == 0) {
return0;
}
intmid = 0;
intbegin = prices[0];
for (inti = 1; i < len; i++) {
if (prices[i] < begin) {
begin = prices[i];
}
if (prices[i] > begin) {
mid = prices[i] - begin > mid ? prices[i] - begin : mid;
}
}
returnmid;
}
}