加载中...
1630-等差子数组(Arithmetic Subarrays)
发表于:2021-12-03 | 分类: 中等
字数统计: 1.2k | 阅读时长: 5分钟 | 阅读量:

原文链接: https://leetcode-cn.com/problems/arithmetic-subarrays

英文原文

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 是等差数列,只需要满足:对于每个有效的 is[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 个整数组成的数组 lr,后两个数组表示 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

 

示例 1:

输入:nums = [4,6,5,9,3,7], l = [0,0,2], r = [2,3,5]
输出:[true,false,true]
解释:
第 0 个查询,对应子数组 [4,6,5] 。可以重新排列为等差数列 [6,5,4] 。
第 1 个查询,对应子数组 [4,6,5,9] 。无法重新排列形成等差数列。
第 2 个查询,对应子数组 [5,9,3,7] 。可以重新排列为等差数列 [3,5,7,9] 。

示例 2:

输入: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]
输出:[false,true,false,false,true,true]

 

提示:

  • 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

通过代码

高赞题解

思路上是一个子数组的其他元素都减去子数组中的最小值, 得到数组D,
如果子数组为等差序列, 那么数组D的最小值d为D的共有因子, D的所有元素都除以d, 就能够构成1, 2, 3, 4,…
如果不能整除或者不能构成1, 2, 3, 4, 那么, 就不是等差序列.
注意, 有一个特例需要判断: 当d = 0的时候, 不能作为分母.
当d = 0的时候, 只需要判断子序列是否是一个常数, 即可.

class Solution:
    def checkArithmeticSubarrays(self, nums: List[int], L: List[int], R: List[int]) -> List[bool]:
        def check(l, r):
            if r - l == 1:
                return True
            A = nums[l : r + 1]
            m = min(A)
            D = []
            for e in A:
                if e != m:
                    D.append(e - m)
            if not D:
                return True
            if len(D) != len(A) - 1:
                return False
            d = min(D)
            vis = [False] * len(D)
            for e in D:
                if e % d:
                    return False
                tmp = e // d - 1
                if tmp < 0 or tmp >= len(D) or vis[tmp]:
                    return False
                else:
                    vis[tmp] = True
            return True


        return [check(l, r) for l, r in zip(L, R)]

但是悲伤的是: 虽然赢了时间复杂度, 但是没有赢下运行时间, 仅仅击败了12%.
原因估计是数据长度太短了(最多500), 时间复杂度上的优势不能体现出来, 而Python自带的sort速度比较快.
如果用其他编程语言, 比如cpp, 我估计效果会非常好.

统计信息

通过次数 提交次数 AC比率
7683 10069 76.3%

提交历史

提交时间 提交结果 执行时间 内存消耗 语言
上一篇:
1629-按键持续时间最长的键(Slowest Key)
下一篇:
1631-最小体力消耗路径(Path With Minimum Effort)
本文目录
本文目录