题解 | #买卖股票的最好时机(一)#TOP80
import java.util.*;
public class Solution {
/**
*
* @param prices int整型一维数组
* @return int整型
*/
public int maxProfit (int[] prices) {
// write code here
if(prices == null ||prices.length == 1){
return 0 ;
}
int buy = prices[0];
int max = Integer.MIN_VALUE;
//买小卖大
for(int i = 1; i< prices.length; i++){
//以最小的价格购买
buy = Math.min(prices[i], buy);
//当前价格 - 购买价格
max = Math.max(max, prices[i] - buy);
}
return max;
}
}