1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class Solution { public int[] findIndices(int[] nums, int indexDifference, int valueDifference) { int n = nums.length; int minIndex = 0, maxIndex = 0; for (int j = indexDifference; j < n; j++) { int i = j - indexDifference; if (nums[i] > nums[maxIndex]) { maxIndex = i; } else if (nums[i] < nums[minIndex]) { minIndex = 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}; } }
|