Last active
December 15, 2025 18:22
-
-
Save tatsuyax25/c974ef395b2ca869ae9c69b686f6651f to your computer and use it in GitHub Desktop.
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 l
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[]} prices | |
| * @return {number} | |
| */ | |
| var getDescentPeriods = function(prices) { | |
| let total = 0; // total number of descent periods | |
| let streak = 0; // length of current descent streak | |
| for (let i = 0; i < prices.length; i++) { | |
| if (i > 0 && prices[i] === prices[i - 1] - 1) { | |
| // continue the descent streak | |
| streak += 1; | |
| } else { | |
| // reset streak (single day always counts) | |
| streak = 1; | |
| } | |
| total += streak; // each day contributes 'streak' periods | |
| } | |
| return total; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment