Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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.
Example 3:
Input: prices = [1] Output: 1 Explanation: There is 1 smooth descent period: [1]
Constraints:
1 <= prices.length <= 1051 <= prices[i] <= 105Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math · Two Pointers · Dynamic Programming · Sliding Window
[3,2,1,4]
[8,6,7,7]
[1]
subarray-product-less-than-k)number-of-valid-subarrays)number-of-zero-filled-subarrays)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2110: Number of Smooth Descent Periods of a Stock
class Solution {
public long getDescentPeriods(int[] prices) {
long ans = 0;
int n = prices.length;
for (int i = 0, j = 0; i < n; i = j) {
j = i + 1;
while (j < n && prices[j - 1] - prices[j] == 1) {
++j;
}
int cnt = j - i;
ans += (1L + cnt) * cnt / 2;
}
return ans;
}
}
// Accepted solution for LeetCode #2110: Number of Smooth Descent Periods of a Stock
func getDescentPeriods(prices []int) (ans int64) {
n := len(prices)
for i, j := 0, 0; i < n; i = j {
j = i + 1
for j < n && prices[j-1]-prices[j] == 1 {
j++
}
cnt := j - i
ans += int64((1 + cnt) * cnt / 2)
}
return
}
# Accepted solution for LeetCode #2110: Number of Smooth Descent Periods of a Stock
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
// Accepted solution for LeetCode #2110: Number of Smooth Descent Periods of a Stock
impl Solution {
pub fn get_descent_periods(prices: Vec<i32>) -> i64 {
let mut ans: i64 = 0;
let n: usize = prices.len();
let mut i: usize = 0;
while i < n {
let mut j: usize = i + 1;
while j < n && prices[j - 1] - prices[j] == 1 {
j += 1;
}
let cnt: i64 = (j - i) as i64;
ans += (1 + cnt) * cnt / 2;
i = j;
}
ans
}
}
// Accepted solution for LeetCode #2110: Number of Smooth Descent Periods of a Stock
function getDescentPeriods(prices: number[]): number {
let ans = 0;
const n = prices.length;
for (let i = 0, j = 0; i < n; i = j) {
j = i + 1;
while (j < n && prices[j - 1] - prices[j] === 1) {
++j;
}
const cnt = j - i;
ans += Math.floor(((1 + cnt) * cnt) / 2);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.