[LeetCode] 2903. Find Indices With Index and Value Difference I

CNoodle發表於2024-05-26

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;
    }
}

思路二 - 滑動視窗

一個更好的思路是使用滑動視窗。這裡我們滑動視窗的尺寸是固定的,為 indexDifference。我們用一個 for 迴圈,遍歷所有可能的 i,然後建立另一個變數 j,使得 j 和 i 的距離一直保持為 indexDifference。在遍歷的過程中,因為 i 和 j 的距離滿足題目的要求了,所以我們可以在遍歷的過程中記錄 j 遇到的最大值和最小值,當走到某個 i 位置的時候,如果發現 nums[i] - min >= valueDifference 或者 max - nums[i] >= valueDifference,我們就可以返回 [minIndex, i] 或者 [maxIndex, i],就說明找到一個可行解了。

複雜度

時間複雜度: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;
    }
}

相關文章