-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBestTime2BuySellStock_121.java
More file actions
31 lines (27 loc) · 986 Bytes
/
BestTime2BuySellStock_121.java
File metadata and controls
31 lines (27 loc) · 986 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
31
/**
* ----------------------------------------------------------------------------
Best Time to Buy and Sell Stock
- Say you have an array for which the ith element is the price of a given
stock on day i.
- If you were only permitted to complete at most one transaction
- (ie, buy one and sell one share of the stock), design an algorithm to
find the maximum profit.
* ----------------------------------------------------------------------------
*/
/**
* XXXXX the min should happen smaller than max
*
* - P[i]: the max profit we can get if selling on day_i
* sell[i] - minprice till day_i
*/
public class Solution {
public int maxProfit(int[] prices) {
if (prices.length == 0) return 0;
int maxP = 0, minBuy = prices[0];
for(int i=1; i<prices.length; i++) {
minBuy = Math.min(minBuy, prices[i]);
maxP = Math.max(maxP, prices[i]-minBuy);
}
return maxP;
}
}