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] <= 105When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy involves checking every possible continuous period of stock prices. We're looking for periods where the price decreases by exactly 1 each day. We simply count the valid periods as we find them.
Here's how the algorithm would work step-by-step:
def get_number_of_smooth_descent_periods(prices):
number_of_prices = len(prices)
number_of_smooth_descent_periods = 0
# Iterate through all possible starting positions
for start_index in range(number_of_prices):
# Iterate through all possible lengths of periods
for period_length in range(1, number_of_prices - start_index + 1):
is_smooth_descent = True
# Check if the current period is a smooth descent.
for index_in_period in range(period_length - 1):
if prices[start_index + index_in_period] - prices[start_index + index_in_period + 1] != 1:
is_smooth_descent = False
break
# Increment count if smooth descent found
if is_smooth_descent:
number_of_smooth_descent_periods += 1
return number_of_smooth_descent_periodsThe problem asks us to count how many periods of time a stock's price decreases smoothly. The key is to avoid recomputing information by building on previous calculations, counting periods as we go. We look at the price drops and smartly keep track of how many smooth periods end at each day, then add them all up.
Here's how the algorithm would work step-by-step:
def get_descent_periods(prices):
number_of_days = len(prices)
descent_periods_ending_here = [1] * number_of_days
total_descent_periods = number_of_days
for day_index in range(1, number_of_days):
# Check if current price continues descent
if prices[day_index] == prices[day_index - 1] - 1:
# Extend descent period from previous day
descent_periods_ending_here[day_index] = \
descent_periods_ending_here[day_index - 1] + 1
# Update total smooth descent periods
total_descent_periods += \
descent_periods_ending_here[day_index] - 1
return total_descent_periods| Case | How to Handle |
|---|---|
| Null or empty input array | Return 0 since there are no price points and therefore no smooth descent periods. |
| Input array with only one element | Return 1 since a single price point is considered a smooth descent period of length 1. |
| Input array with prices in strictly descending order | The result should be a sum of consecutive integers from 1 to n where n is the length of the array. |
| Input array with all identical prices | Each individual day is a smooth descent period, so the result should equal the length of the array. |
| Input array with prices in strictly ascending order | The result should equal the length of the array, since each day is individually a smooth descent period. |
| Input array with alternating increasing and decreasing prices (e.g., [1, 2, 1, 2, 1]) | The result should correspond to the number of individual days plus the count of descending pairs with difference of 1. |
| Very large input array (approaching memory limits) | The algorithm should use O(1) space, avoiding large auxiliary data structures and preventing out-of-memory errors. |
| Integer overflow when calculating the number of periods | Use a 64-bit integer (long) to accumulate the count of smooth descent periods. |