LeetCode 题解工作台

等差数列划分

如果一个数列 至少有三个元素 ,并且任意两个相邻元素之差相同,则称该数列为等差数列。 例如, [1,3,5,7,9] 、 [7,7,7,7] 和 [3,-1,-5,-9] 都是等差数列。 给你一个整数数组 nums ,返回数组 nums 中所有为等差数组的 子数组 个数。 子数组 是数组中的一个连续…

category

3

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 状态·转移·动态规划

bolt

答案摘要

我们用 表示当前相邻两个元素的差值,用 表示当前等差数列的长度,初始时 $d = 3000$, $cnt = 2$。 遍历数组 `nums`,对于相邻的两个元素 和 ,如果 $b - a = d$,则说明当前元素 也属于当前等差数列,此时 自增 1;否则说明当前元素 不属于当前等差数列,此时更新 $d = b - a$,且 $cnt = 2$。如果 $cnt \ge 3$,则说明当前等…

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

如果一个数列 至少有三个元素 ,并且任意两个相邻元素之差相同,则称该数列为等差数列。

  • 例如,[1,3,5,7,9][7,7,7,7][3,-1,-5,-9] 都是等差数列。

给你一个整数数组 nums ,返回数组 nums 中所有为等差数组的 子数组 个数。

子数组 是数组中的一个连续序列。

 

示例 1:

输入:nums = [1,2,3,4]
输出:3
解释:nums 中有三个子等差数组:[1, 2, 3]、[2, 3, 4] 和 [1,2,3,4] 自身。

示例 2:

输入:nums = [1]
输出:0

 

提示:

  • 1 <= nums.length <= 5000
  • -1000 <= nums[i] <= 1000
lightbulb

解题思路

方法一:遍历计数

我们用 dd 表示当前相邻两个元素的差值,用 cntcnt 表示当前等差数列的长度,初始时 d=3000d = 3000, cnt=2cnt = 2

遍历数组 nums,对于相邻的两个元素 aabb,如果 ba=db - a = d,则说明当前元素 bb 也属于当前等差数列,此时 cntcnt 自增 1;否则说明当前元素 bb 不属于当前等差数列,此时更新 d=bad = b - a,且 cnt=2cnt = 2。如果 cnt3cnt \ge 3,则说明当前等差数列的长度至少为 3,此时等差数列的个数为 cnt2cnt - 2,将其加到答案中。

遍历结束后,即可得到答案。

在代码实现上,我们也可以将 cntcnt 初始化为 00,重置 cntcnt 时,直接将 cntcnt 置为 00;累加答案时,直接累加 cntcnt 即可。

时间复杂度 O(n)O(n),空间复杂度 O(1)O(1)。其中 nn 是数组 nums 的长度。

相似题目:

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
    def numberOfArithmeticSlices(self, nums: List[int]) -> int:
        ans = cnt = 0
        d = 3000
        for a, b in pairwise(nums):
            if b - a == d:
                cnt += 1
            else:
                d = b - a
                cnt = 0
            ans += cnt
        return ans
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Check if the candidate can optimize the solution using dynamic programming and state transition techniques.

  • question_mark

    Assess the candidate's ability to optimize space complexity, particularly when handling large input sizes.

  • question_mark

    Look for a clear understanding of sliding window techniques and how they can apply to sequence-based problems.

warning

常见陷阱

外企场景
  • error

    Failing to account for subarrays with fewer than three elements, which are not considered arithmetic subarrays.

  • error

    Incorrectly updating the count of arithmetic subarrays, leading to over-counting or under-counting.

  • error

    Using brute-force methods to check each possible subarray, resulting in an inefficient solution.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    What if the array contains only two elements? How would the solution change?

  • arrow_right_alt

    Can we solve this problem with a more memory-efficient approach while maintaining optimal time complexity?

  • arrow_right_alt

    What happens if the array contains large negative or positive numbers? How would the approach handle extreme values?

help

常见问题

外企场景

等差数列划分题解:状态·转移·动态规划 | LeetCode #413 中等