【算法题】2905. 找出满足差值条件的下标 II

news/2024/7/23 19:10:36 标签: 算法, leetcode, 数据结构

插: 前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。
坚持不懈,越努力越幸运,大家一起学习鸭~~~

题目:

给你一个下标从 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 可能 相等 。

示例 1:

输入:nums = [5,1,4,1], indexDifference = 2, valueDifference = 4
输出:[0,3]
解释:在示例中,可以选择 i = 0 和 j = 3 。
abs(0 - 3) >= 2 且 abs(nums[0] - nums[3]) >= 4 。
因此,[0,3] 是一个符合题目要求的答案。
[3,0] 也是符合题目要求的答案。
示例 2:

输入:nums = [2,1], indexDifference = 0, valueDifference = 0
输出:[0,0]
解释:
在示例中,可以选择 i = 0 和 j = 0 。
abs(0 - 0) >= 0 且 abs(nums[0] - nums[0]) >= 0 。
因此,[0,0] 是一个符合题目要求的答案。
[0,1]、[1,0] 和 [1,1] 也是符合题目要求的答案。
示例 3:

输入:nums = [1,2,3], indexDifference = 2, valueDifference = 4
输出:[-1,-1]
解释:在示例中,可以证明无法找出 2 个满足所有条件的下标。
因此,返回 [-1,-1] 。

提示:

1 <= n == nums.length <= 10^5
0 <= nums[i] <= 10^9
0 <= indexDifference <= 10^5
0 <= valueDifference <= 10^9

java代码:

class Solution {
  //双指针+维护最大最小值
    public int[] findIndices(int[] nums, int indexDifference, int valueDifference) {
        int maxIdx = 0, minIdx = 0;
        for (int j = indexDifference; j < nums.length; j++) {
            int i = j - indexDifference;
            if (nums[i] > nums[maxIdx]) {
                maxIdx = i;
            } else if (nums[i] < nums[minIdx]) {
                minIdx = i;
            }
            if (nums[maxIdx] - nums[j] >= valueDifference) {
                return new int[]{maxIdx, j};
            }
            if (nums[j] - nums[minIdx] >= valueDifference) {
                return new int[]{minIdx, j};
            }
        }
        return new int[]{-1, -1};
    }
}


http://www.niftyadmin.cn/n/5098999.html

相关文章

如何使用 OpenSSL 来检查证书,来确保网络通信的安全性?

OpenSSL 是一个强大的安全套接字层密码库&#xff0c;包含丰富的加密算法、常用的密钥和证书封装管理功能以及 SSL/TLS 协议&#xff0c;并提供了丰富的应用程序供测试或其他目的使用。要使用 OpenSSL 来检查证书以确保网络通信的安全性&#xff0c;您可以遵循以下步骤&#xf…

基于 Servlet 的博客系统

基于 Servlet 的博客系统 一、准备工作1、创建项目2、创建包3、导入前端静态页面 二、数据库设计1、blog&#xff08;博客表&#xff09;2、user&#xff08;用户表&#xff09;3、建库建表的 SQL 语句 三、封装数据库操作1、为什么要封装数据库&#xff1f;2、封装数据库的连接…

第六十章 符号概览

文章目录 第六十章 符号概览“单词”中间的非字母数字字符abc^defi%abcdefabc->defabc?def 第六十章 符号概览 “单词”中间的非字母数字字符 本节列出了看起来像带有非字母数字字符的单词的表单。其中许多是显而易见的&#xff0c;因为操作员很熟悉。例如&#xff1a; …

面试经典150题——Day13

文章目录 一、题目二、题解 一、题目 238. Product of Array Except Self Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. The product of any prefix or suffix of nums …

LeetCode75——Day8

文章目录 一、题目二、题解 一、题目 334. Increasing Triplet Subsequence Given an integer array nums, return true if there exists a triple of indices (i, j, k) such that i < j < k and nums[i] < nums[j] < nums[k]. If no such indices exists, retu…

LCR 182.动态口令

​​题目来源&#xff1a; leetcode题目&#xff0c;网址&#xff1a;LCR 182. 动态口令 - 力扣&#xff08;LeetCode&#xff09; 解题思路&#xff1a; 按要求截取子字符串并拼接即可。 解题代码&#xff1a; class Solution { public:string dynamicPassword(string passw…

【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中

![请 https://cloud.tencent.com/act/cps/redirect?redirect2446&cps_key2e531299bf7e92946df4c3162a81b552&fromconsole

【RocketMQ系列六】RocketMQ事务消息

您好&#xff0c;我是码农飞哥&#xff08;wei158556&#xff09;&#xff0c;感谢您阅读本文&#xff0c;欢迎一键三连哦。 &#x1f4aa;&#x1f3fb; 1. Python基础专栏&#xff0c;基础知识一网打尽&#xff0c;9.9元买不了吃亏&#xff0c;买不了上当。 Python从入门到精…