2110. Number of Smooth Descent Periods of a Stock LeetCode Solution

In this guide, you will get 2110. Number of Smooth Descent Periods of a Stock LeetCode Solution with the best time and space complexity. The solution to Number of Smooth Descent Periods of a Stock problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Number of Smooth Descent Periods of a Stock solution in C++
  4. Number of Smooth Descent Periods of a Stock solution in Java
  5. Number of Smooth Descent Periods of a Stock solution in Python
  6. Additional Resources
2110. Number of Smooth Descent Periods of a Stock LeetCode Solution image

Problem Statement of Number of Smooth Descent Periods of a Stock

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 one or more contiguous days such that the price on each day is lower than the price on the preceding day by exactly 1. The first day of the period is exempted from this rule.
Return the number of smooth descent periods.

Example 1:

Input: prices = [3,2,1,4]
Output: 7
Explanation: There are 7 smooth descent periods:
[3], [2], [1], [4], [3,2], [2,1], and [3,2,1]
Note that a period with one day is a smooth descent period by the definition.

Example 2:

Input: prices = [8,6,7,7]
Output: 4
Explanation: There are 4 smooth descent periods: [8], [6], [7], and [7]
Note that [8,6] is not a smooth descent period as 8 – 6 ≠ 1.

See also  1470. Shuffle the Array LeetCode Solution

Example 3:

Input: prices = [1]
Output: 1
Explanation: There is 1 smooth descent period: [1]

Constraints:

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2110. Number of Smooth Descent Periods of a Stock LeetCode Solution in C++

class Solution {
 public:
  long long getDescentPeriods(vector<int>& prices) {
    long ans = 1;  // prices[0]
    int dp = 1;

    for (int i = 1; i < prices.size(); ++i) {
      if (prices[i] == prices[i - 1] - 1)
        ++dp;
      else
        dp = 1;
      ans += dp;
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

2110. Number of Smooth Descent Periods of a Stock LeetCode Solution in Java

class Solution {
  public long getDescentPeriods(int[] prices) {
    long ans = 1; // prices[0]
    int dp = 1;

    for (int i = 1; i < prices.size(); ++i) {
      if (prices[i] == prices[i - 1] - 1)
        ++dp;
      else
        dp = 1;
      ans += dp;
    }

    return ans;
  }
}
// code provided by PROGIEZ

2110. Number of Smooth Descent Periods of a Stock LeetCode Solution in Python

class Solution:
  def getDescentPeriods(self, prices: list[int]) -> int:
    ans = 1  # prices[0]
    dp = 1

    for i in range(1, len(prices)):
      if prices[i] == prices[i - 1] - 1:
        dp += 1
      else:
        dp = 1
      ans += dp

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.