2903. Find Indices With Index and Value Difference I

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public int[] findIndices(int[] nums, int indexDifference, int valueDifference) {
int minIndex = 0, maxIndex = 0;

// 保证索引差满足题目要求
for (int j = indexDifference; j < nums.length; j++) {
int i = j - indexDifference;
if (nums[i] < nums[minIndex]) {
minIndex = i;
} else if (nums[i] > nums[maxIndex]) {
maxIndex = i;
}

if (nums[maxIndex] - nums[j] >= valueDifference) {
return new int[]{maxIndex, j};
}
if (nums[j] - nums[minIndex] >= valueDifference) {
return new int[]{minIndex, j};
}
}

return new int[]{-1, -1};
}
}

References

2903. Find Indices With Index and Value Difference I