首页 > 其他分享 >[LeetCode] 1630. Arithmetic Subarrays

[LeetCode] 1630. Arithmetic Subarrays

时间:2023-11-24 09:12:56浏览次数:34  
标签:1630 nums int Subarrays sequence list true LeetCode arithmetic

A sequence of numbers is called arithmetic if it consists of at least two elements, and the difference between every two consecutive elements is the same. More formally, a sequence s is arithmetic if and only if s[i+1] - s[i] == s[1] - s[0] for all valid i.

For example, these are arithmetic sequences:
1, 3, 5, 7, 9
7, 7, 7, 7
3, -1, -5, -9

The following sequence is not arithmetic:
1, 1, 2, 5, 7

You are given an array of n integers, nums, and two arrays of m integers each, l and r, representing the m range queries, where the ith query is the range [l[i], r[i]]. All the arrays are 0-indexed.

Return a list of boolean elements answer, where answer[i] is true if the subarray nums[l[i]], nums[l[i]+1], ... , nums[r[i]] can be rearranged to form an arithmetic sequence, and false otherwise.

Example 1:
Input: nums = [4,6,5,9,3,7], l = [0,0,2], r = [2,3,5]
Output: [true,false,true]
Explanation:
In the 0th query, the subarray is [4,6,5]. This can be rearranged as [6,5,4], which is an arithmetic sequence.
In the 1st query, the subarray is [4,6,5,9]. This cannot be rearranged as an arithmetic sequence.
In the 2nd query, the subarray is [5,9,3,7]. This can be rearranged as [3,5,7,9], which is an arithmetic sequence.

Example 2:
Input: nums = [-12,-9,-3,-12,-6,15,20,-25,-20,-15,-10], l = [0,1,6,4,8,7], r = [4,4,9,7,9,10]
Output: [false,true,false,false,true,true]

Constraints:
n == nums.length
m == l.length
m == r.length
2 <= n <= 500
1 <= m <= 500
0 <= l[i] < r[i] < n
-105 <= nums[i] <= 105

等差子数组。

如果一个数列由至少两个元素组成,且每两个连续元素之间的差值都相同,那么这个序列就是 等差数列 。更正式地,数列 s 是等差数列,只需要满足:对于每个有效的 i , s[i+1] - s[i] == s[1] - s[0] 都成立。

例如,下面这些都是 等差数列 :
1, 3, 5, 7, 9
7, 7, 7, 7
3, -1, -5, -9

下面的数列 不是等差数列 :
1, 1, 2, 5, 7
给你一个由 n 个整数组成的数组 nums,和两个由 m 个整数组成的数组 l 和 r,后两个数组表示 m 组范围查询,其中第 i 个查询对应范围 [l[i], r[i]] 。所有数组的下标都是 从 0 开始 的。

返回 boolean 元素构成的答案列表 answer 。如果子数组 nums[l[i]], nums[l[i]+1], ... , nums[r[i]] 可以 重新排列 形成 等差数列 ,answer[i] 的值就是 true;否则answer[i] 的值就是 false 。

思路

根据题意,我们可以从 nums 中挑出范围在 [l, r] 的元素,对这部分元素排序,然后判断这部分元素是否为等差数列,如果是则往结果集放入True,否则放入False。

复杂度

时间O(m^2 * logm) - 假设 [l, r] 这一段元素长度为 m,对这一段元素排序是O(mlogm),判断这一段元素是否为等差数列最差可以到达O(m),所以整体是O(m^2 * logm)
空间O(n) - output list

代码

Java实现

class Solution {
    public List<Boolean> checkArithmeticSubarrays(int[] nums, int[] l, int[] r) {
        List<Boolean> res = new ArrayList<>();
        int n = l.length;
        for (int i = 0; i < n; i++) {
            int start = l[i];
            int end = r[i];
            List<Integer> list = new ArrayList<>();
            for (int j = start; j <= end; j++) {
                list.add(nums[j]);
            }
            Collections.sort(list);
            if (helper(list)) {
                res.add(true);
            } else {
                res.add(false);
            }
        }
        return res;
    }

    private boolean helper(List<Integer> list) {
        int size = list.size();
        for (int i = 2; i < size; i++) {
            if (list.get(i) - list.get(i - 1) != list.get(i - 1) - list.get(i - 2)) {
                return false;
            }
        }
        return true;
    }
}

标签:1630,nums,int,Subarrays,sequence,list,true,LeetCode,arithmetic
From: https://www.cnblogs.com/cnoodle/p/17852951.html

相关文章

  • LeetCode之二叉树
    发现新天地,欢迎访问Cr不是铬的个人网站平衡二叉树做这一道题目我们要考虑到平衡二叉树的定义。也就是一个二叉树每个节点的左右两个子树的高度差的绝对值不超过1。关于一个结点的高度计算我们很容易用递归得出,那么我们用递归遍历加上这个判断条件即可.classSolution{pu......
  • 【11月LeetCode组队打卡】Task3--BinaryTree
    树基本术语:节点的度:叶子节点=0分支节点:含有的子树个数节点关系:父,子,兄节点层次:根节点:1floor路径:两节点间经过的节点序列路径长度:路径上的边数树的分类:节点子树是否可以互换位置:有序树:从左到右各子树依次有序(不能互换无序树二叉......
  • LeetCode-Java:88合并两个有序数组
    题目:给你两个按非递减顺序排列的整数数组nums1和nums2,另有两个整数m和n,分别表示nums1和nums2中的元素数目。请你合并nums2到nums1中,使合并后的数组同样按非递减顺序排列。注意:最终,合并后数组不应由函数返回,而是存储在数组nums1中。为了应对这种情况,nums1......
  • leetcode324场周赛
    一、使三个字符串相等给你三个字符串s1、s2和s3。你可以根据需要对这三个字符串执行以下操作任意次数。在每次操作中,你可以选择其中一个长度至少为2的字符串并删除其最右位置上的字符。如果存在某种方法能够使这三个字符串相等,请返回使它们相等所需的最小操作次数......
  • [LeetCode] 1361. Validate Binary Tree Nodes 验证二叉树
    Youhave n binarytreenodesnumberedfrom 0 to n-1 wherenode i hastwochildren leftChild[i] and rightChild[i],return true ifandonlyif all thegivennodesform exactlyone validbinarytree.Ifnode i hasnoleftchildthen leftCh......
  • 【11月LeetCode组队打卡】Task2--TrieTree
    字典树Trie音同try,又称前缀树,是一颗有根树,根节点到树节点的一个路径就代表一个单词,多用于关键词检索,自动补完和拼写检查用空间换时间:借公共前缀来降低查询时间的开销根节点无内容(参考:字典树TrieTree图文详解——CSDN实现Trie题解——力扣)208.实现Trie复习一下this......
  • 【11月LeetCode组队打卡】Task2--String & StringMatch
    在CSP里面好多道“水题“基本都离不开字符串/数组的模拟滚动哈希,字典树,DP几个强强联合基本可以横扫所有难度的字符串算法了,所以在这个task里会好好消化其中前二字符串和数组有很多相似之处,比如同样使用下标的方式来访问单个字符。根据字符串的特点,将字符串问题分为以下几种:字......
  • 【DP】Leetcode 322 Coin Change
    题目链接322.零钱兑换思路因为硬币数量是无限的,所以无法以硬币数量作为状态进行遍历代码classSolution{publicintcoinChange(int[]coins,intamount){intn=coins.length;if(n==0){return-1;}//dp[i]......
  • LeetCode之二叉树
    发现更多计算机知识,欢迎访问Cr不是铬的个人网站最近数据结构学到二叉树,就刷了刷力扣,写这篇文章也是辅助记忆。103二叉树锯齿形遍历要解出本道题,首先要会层次遍历。层次遍历我们都知道用一个队列去实现就行。但是力扣这里的输出时一个二维的vector,每一层的值在不同的列表里面......
  • 【11月LeetCode组队打卡】Task1--HashTable
    在准备CSP,借这次组队打卡的机会好好复习一下cpp的各种基操(微操,和基础的数据结构217.存在重复元素vector向量的用法有点忘了,先简单回顾一下(其实是好久没写cpp了(安详.jpg输入与输出//未知数组元素个数vector<int>hash;intx;while(cin>>x){hash......