2016. 增量元素之间的最大差值
链接:https://leetcode-cn.com/problems/maximum-difference-between-increasing-elements/
思路:维护一个前缀最小值和最大差值
class Solution {
public int maximumDifference(int[] nums) {
//前缀最小值 最大差值
int min = nums[0], result = -1;
for (int i = 1; i < nums.length; i++) {
if (nums[i] <= min) {
min = nums[i];
} else {
result = Math.max(result,nums[i]-min);
}
}
return result;
}
}
做完这个题之后又想起了股票问题,二刷股票
class Solution {
public int maxProfit(int[] prices) {
int profit = 0, minPrice = prices[0];
for (int i = 0; i < prices.length; i++) {
if (prices[i] <= minPrice) {
minPrice = prices[i];
}else {
profit = Math.max(profit,prices[i]-minPrice);
}
}
return profit;
}
}
此处评论已关闭