暴力【超时】
class Solution {
public int maxProfit(int[] prices) {
int max = 0;
for(int i=0; i<prices.length; i++){
for(int j=i+1; j<prices.length; j++){
if(prices[j]-prices[i] > max){
max = prices[j] - prices[i];
}
}
}
return max;
}
}
一次遍历
class Solution {
public int maxProfit(int[] prices) {
int min = Integer.MAX_VALUE;
int max = 0;
for(int i=0; i<prices.length; i++){
if(prices[i] < min){
min = prices[i];
}else if(prices[i] - min > max){
max = prices[i] - min;
}
}
return max;
}
}