[LeetCode] 2903. Find Indices With Index and Value Difference I
You are given a 0-indexed integer array nums having length n, an integer indexDifference, and an integer valueDifference.
Your task is to find two indices i and j, both in the range [0, n - 1], that satisfy the following conditions:
abs(i - j) >= indexDifference, and
abs(nums[i] - nums[j]) >= valueDifference
Return an integer array answer, where answer = [i, j] if there are two such indices, and answer = [-1, -1] otherwise. If there are multiple choices for the two indices, return any of them.
Note: i and j may be equal.
Example 1:
Input: nums = [5,1,4,1], indexDifference = 2, valueDifference = 4
Output: [0,3]
Explanation: In this example, i = 0 and j = 3 can be selected.
abs(0 - 3) >= 2 and abs(nums[0] - nums[3]) >= 4.
Hence, a valid answer is [0,3].
[3,0] is also a valid answer.
Example 2:
Input: nums = [2,1], indexDifference = 0, valueDifference = 0
Output: [0,0]
Explanation: In this example, i = 0 and j = 0 can be selected.
abs(0 - 0) >= 0 and abs(nums[0] - nums[0]) >= 0.
Hence, a valid answer is [0,0].
Other valid answers are [0,1], [1,0], and [1,1].
Example 3:
Input: nums = [1,2,3], indexDifference = 2, valueDifference = 4
Output: [-1,-1]
Explanation: In this example, it can be shown that it is impossible to find two indices that satisfy both conditions.
Hence, [-1,-1] is returned.
Constraints:
1 <= n == nums.length <= 100
0 <= nums[i] <= 50
0 <= indexDifference <= 100
0 <= valueDifference <= 50
找出满足差值条件的下标 I。
给你一个下标从 0 开始、长度为 n 的整数数组 nums ,以及整数 indexDifference 和整数 valueDifference 。你的任务是从范围 [0, n - 1] 内找出 2 个满足下述所有条件的下标 i 和 j :
abs(i - j) >= indexDifference 且
abs(nums[i] - nums[j]) >= valueDifference
返回整数数组 answer。如果存在满足题目要求的两个下标,则 answer = [i, j] ;否则,answer = [-1, -1] 。如果存在多组可供选择的下标对,只需要返回其中任意一组即可。注意:i 和 j 可能 相等 。
思路一 - 暴力解
两层 for 循环,遍历所有可能的 i 和 j,判断是否满足条件。
复杂度
时间复杂度:O(n^2)
空间复杂度:O(1)
代码
Java实现
class Solution {
public int[] findIndices(int[] nums, int indexDifference, int valueDifference) {
int n = nums.length;
int[] res = new int[] { -1, -1 };
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (Math.abs(i - j) >= indexDifference && Math.abs(nums[i] - nums[j]) >= valueDifference) {
res[0] = i;
res[1] = j;
}
}
}
return res;
}
}
思路二 - 滑动窗口
我参考了这个帖子。
设我们要找的两个下标 i 和 j,i 在左,j 在右,i <= j - indexDifference。
在指针往前走的时候,我们一路上记录一下遇到的最大值,最小值和他们各自的下标,记为 max,min,maxIndex,minIndex。那么对于 j 指针来说,他只要往他的左侧 indexDifference 个位置看,看是否存在一个下标和值满足题意即可。
这个思路可以用到版本二 2905 题,是一模一样的思路。
复杂度
时间O(n)
空间O(1)
代码
Java实现
class Solution {
public int[] findIndices(int[] nums, int indexDifference, int valueDifference) {
int[] res = new int[] { -1, -1 };
int j;
int max = nums[0];
int min = nums[0];
int maxIndex = 0;
int minIndex = 0;
for (int i = indexDifference; i < nums.length; i++) {
j = i - indexDifference;
if (nums[j] > max) {
max = nums[j];
maxIndex = j;
}
if (nums[j] < min) {
min = nums[j];
minIndex = j;
}
if (nums[i] - min >= valueDifference) {
return new int[] { minIndex, i };
}
if (max - nums[i] >= valueDifference) {
return new int[] { maxIndex, i };
}
}
return res;
}
}