Leetcode 2110: Number of Smooth Descent Periods of a Stock

grid47
grid47
Exploring patterns and algorithms
Apr 10, 2024 5 min read

You are given an integer array prices representing the daily price history of a stock, where prices[i] is the stock price on the ith day. A smooth descent period of a stock consists of contiguous days such that the price on each day decreases by exactly 1 from the previous day. The first day of the period can be a single-day smooth descent. Return the total number of smooth descent periods.
Problem
Approach
Steps
Complexity
Input: You are given a list of integers representing the stock prices over multiple days.
Example: prices = [5, 4, 3, 6]
Constraints:
• 1 <= prices.length <= 10^5
• 1 <= prices[i] <= 10^5
Output: Return the number of smooth descent periods where each period consists of one or more contiguous days where the price decreases by exactly 1 each day.
Example: For prices = [5, 4, 3, 6], the output should be 6.
Constraints:
Goal: The goal is to calculate the number of smooth descent periods from the given stock price list.
Steps:
• Initialize a variable to keep track of the current descent period length.
• Iterate through the prices list, and for each day, check if the price is exactly 1 less than the previous day's price.
• For each valid descent period, increment the count of descent periods.
Goal: Ensure that the solution can handle large input sizes efficiently.
Steps:
• The input list can be as large as 10^5 elements.
Assumptions:
• The list of prices contains only positive integers.
• The stock price list can have both increasing and decreasing price sequences.
Input: Example 1: prices = [5, 4, 3, 6]
Explanation: The smooth descent periods are: [5], [4], [3], [6], [5,4], and [4,3], resulting in a total of 6 smooth descent periods.

Input: Example 2: prices = [8, 6, 5, 7]
Explanation: The smooth descent periods are: [8], [6], [5], and [7]. There are no multi-day descent periods here.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus