java 代码
class Solution {
public int maxDiff(int[] nums) {
if (nums == null || nums.length <= 1) return 0;
int buyPrice = nums[0];
int maxProfit = Integer.MIN_VALUE;
for (int i = 1; i < nums.length; i++) {
if (nums[i] - buyPrice > maxProfit)
maxProfit = nums[i] - buyPrice;
if (nums[i] < buyPrice) buyPrice = nums[i];
}
return (maxProfit > 0) ? maxProfit : 0;
}
}