121. best time to buy and sell stock

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public int maxProfit(int[] prices) {
if (prices.length==0){
return 0;
}
int min = prices[0];
int max = 0;
for(int price : prices ){
if (price-min>max){
max = price-min;
}
if (price<min){
min = price;
}
}
return max;
}
}