Day12力扣打卡

打卡记录

Day12力扣打卡_第1张图片


找出满足差值条件的下标 II(双指针+维护最大最小)

链接

采用双指针保留间隔 indexDifference 进行遍历,求出慢指针对应一路遍历过来的最大值和最小值。

class Solution {
public:
    vector<int> findIndices(vector<int>& nums, int indexDifference, int valueDifference) {
        int min_idx = 0, max_idx = 0;
        for (int i = indexDifference; i < nums.size(); ++i) {
            int j = i - indexDifference;
            if (nums[j] > nums[max_idx]) max_idx = j;
            if (nums[j] < nums[min_idx]) min_idx = j;
            if (nums[i] - nums[min_idx] >= valueDifference) return {min_idx, i}; 
            if (nums[max_idx] - nums[i] >= valueDifference) return {i, max_idx};
        }
        return {-1, -1};
    }
};

你可能感兴趣的:(leetcode刷题打卡,leetcode,算法,c++)