[LeetCode] 1630. Arithmetic Subarrays

发布时间 2023-11-24 08:57:56作者: CNoodle

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