forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_121.java
27 lines (25 loc) · 876 Bytes
/
_121.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
package com.fishercoder.solutions;
public class _121 {
public static class Solution1 {
/**
* The key here is that you'll have to buy first, before you can sell. That means, if the lower
* price comes after a higher price, their combination won't work! Since you cannot sell first
* before you buy it.
*/
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) {
return 0;
}
int minBuy = prices[0];
int maxProfit = 0;
for (int i = 1; i < prices.length; i++) {
if (prices[i] < minBuy) {
minBuy = prices[i];
} else {
maxProfit = Math.max(maxProfit, prices[i] - minBuy);
}
}
return maxProfit;
}
}
}