-
Notifications
You must be signed in to change notification settings - Fork 1
/
MaximumDifferenceBetweenIncreasingElements.java
53 lines (51 loc) · 1.75 KB
/
MaximumDifferenceBetweenIncreasingElements.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.hncboy;
/**
* @author hncboy
* @date 2022/2/26 16:45
* 2016.增量元素之间的最大差值
*
* 给你一个下标从 0 开始的整数数组 nums ,该数组的大小为 n ,请你计算 nums[j] - nums[i] 能求得的 最大差值 ,其中 0 <= i < j < n 且 nums[i] < nums[j] 。
* 返回 最大差值 。如果不存在满足要求的 i 和 j ,返回 -1 。
*
* 示例 1:
* 输入:nums = [7,1,5,4]
* 输出:4
* 解释:
* 最大差值出现在 i = 1 且 j = 2 时,nums[j] - nums[i] = 5 - 1 = 4 。
* 注意,尽管 i = 1 且 j = 0 时 ,nums[j] - nums[i] = 7 - 1 = 6 > 4 ,但 i > j 不满足题面要求,所以 6 不是有效的答案。
*
* 示例 2:
* 输入:nums = [9,4,3,2]
* 输出:-1
* 解释:
* 不存在同时满足 i < j 和 nums[i] < nums[j] 这两个条件的 i, j 组合。
*
* 示例 3:
* 输入:nums = [1,5,2,10]
* 输出:9
* 解释:
* 最大差值出现在 i = 0 且 j = 3 时,nums[j] - nums[i] = 10 - 1 = 9 。
*
* 提示:
* n == nums.length
* 2 <= n <= 1000
* 1 <= nums[i] <= 109
* 通过次数 27,354 提交次数 45,586
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/maximum-difference-between-increasing-elements
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
public class MaximumDifferenceBetweenIncreasingElements {
public int maximumDifference(int[] nums) {
int result = -1;
// 维护 i 左侧的最小值
for (int i = 0, min = nums[0]; i < nums.length; i++) {
if (nums[i] > min) {
result = Math.max(result, nums[i] - min);
}
min = Math.min(min, nums[i]);
}
return result;
}
}