Skip to content
Discussion options

You must be logged in to vote

We need to count contiguous subarrays where each element decreases by exactly 1 from the previous element. Let me break down the solution approach.

Approach:

  • Use a dynamic programming approach to track smooth descent periods ending at each index
  • For each day i, count the number of valid smooth descent periods that end at that day
  • If the price difference from the previous day is exactly -1, extend the periods from the previous day
  • Otherwise, start a new period (only the current day)

Let's implement this solution in PHP: 2110. Number of Smooth Descent Periods of a Stock

<?php
/**
 * @param Integer[] $prices
 * @return Integer
 */
function getDescentPeriods($prices) {
    $n = count($prices)…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Dec 15, 2025
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants