LeetCode 题解工作台

股票平滑下跌阶段的数目

给你一个整数数组 prices ,表示一支股票的历史每日股价,其中 prices[i] 是这支股票第 i 天的价格。 一个 平滑下降的阶段 定义为:对于 连续一天或者多天 ,每日股价都比 前一日股价恰好少 1 ,这个阶段第一天的股价没有限制。 请你返回 平滑下降阶段 的数目。 示例 1: 输入: p…

category

3

题型

code_blocks

7

代码语言

hub

3

相关题

当前训练重点

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

bolt

答案摘要

我们定义一个答案变量 ,初始值为 。 接下来,我们使用双指针 和 ,分别指向当前平滑下降阶段的第一天和最后一天的下一天。初始时 $i = 0$, $j = 0$。

Interview AiBox logo

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

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个整数数组 prices ,表示一支股票的历史每日股价,其中 prices[i] 是这支股票第 i 天的价格。

一个 平滑下降的阶段 定义为:对于 连续一天或者多天 ,每日股价都比 前一日股价恰好少 1 ,这个阶段第一天的股价没有限制。

请你返回 平滑下降阶段 的数目。

 

示例 1:

输入:prices = [3,2,1,4]
输出:7
解释:总共有 7 个平滑下降阶段:
[3], [2], [1], [4], [3,2], [2,1] 和 [3,2,1]
注意,仅一天按照定义也是平滑下降阶段。

示例 2:

输入:prices = [8,6,7,7]
输出:4
解释:总共有 4 个连续平滑下降阶段:[8], [6], [7] 和 [7]
由于 8 - 6 ≠ 1 ,所以 [8,6] 不是平滑下降阶段。

示例 3:

输入:prices = [1]
输出:1
解释:总共有 1 个平滑下降阶段:[1]

 

提示:

  • 1 <= prices.length <= 105
  • 1 <= prices[i] <= 105
lightbulb

解题思路

方法一:双指针

我们定义一个答案变量 ans\textit{ans},初始值为 00

接下来,我们使用双指针 iijj,分别指向当前平滑下降阶段的第一天和最后一天的下一天。初始时 i=0i = 0, j=0j = 0

从左到右遍历数组 prices\textit{prices},对于每个位置 ii,我们将 jj 向右移动,直到 jj 到达数组末尾或者 prices[j1]prices[j]1\textit{prices}[j - 1] - \textit{prices}[j] \neq 1 为止。此时,cnt=ji\textit{cnt} = j - i 即为当前平滑下降阶段的长度,我们累加 (1+cnt)×cnt2\frac{(1 + \textit{cnt}) \times \textit{cnt}}{2} 到答案变量 ans\textit{ans} 中。接下来将 ii 更新为 jj,继续遍历。

遍历结束后,返回答案变量 ans\textit{ans} 即可。

时间复杂度 O(n)O(n),其中 nn 为数组 prices\textit{prices} 的长度。空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
    def getDescentPeriods(self, prices: List[int]) -> int:
        ans = 0
        i, n = 0, len(prices)
        while i < n:
            j = i + 1
            while j < n and prices[j - 1] - prices[j] == 1:
                j += 1
            cnt = j - i
            ans += (1 + cnt) * cnt // 2
            i = j
        return ans
speed

复杂度分析

指标
时间complexity is O(n) since we iterate through the prices array once. Space complexity is O(1) because we only track current period length and total count without storing intermediate periods.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Focuses on consecutive differences of exactly 1 in array sequences.

  • question_mark

    Expect candidates to handle single-day periods correctly.

  • question_mark

    Wants an O(n) one-pass solution without extra arrays.

warning

常见陷阱

外企场景
  • error

    Resetting the period length incorrectly when difference is not exactly 1.

  • error

    Forgetting to include single-day periods in the total count.

  • error

    Miscounting overlapping sub-periods within longer smooth descent sequences.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Count smooth ascent periods where each day's price increases by 1.

  • arrow_right_alt

    Calculate maximum length of any smooth descent period instead of total count.

  • arrow_right_alt

    Handle circular arrays where the end connects back to the start for smooth descent.

help

常见问题

外企场景

股票平滑下跌阶段的数目题解:状态·转移·动态规划 | LeetCode #2110 中等