Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/2110.html
2110. Number of Smooth Descent Periods of a Stock (Medium)
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 <= 105
1 <= prices[i] <= 105
Similar Questions:
Solution 1.
Get the length of each segment that A[i] == A[i-1] - 1
. If a segment is of length len
, then it contributes 1 + 2 + 3 + ... + len = (1 + len) * len / 2
subarrays into the answer. For example [3,2,1]
has [3,2,1]
(1 array of length 3), [3,2]
, [2,1]
(2 arrays of length 2), [3]
, [2]
, [1]
(3 arrays of length 1), in total 1 + 2 + 3 = 6
valid subarrays.
-
// OJ: https://leetcode.com/problems/number-of-smooth-descent-periods-of-a-stock/ // Time: O(N) // Space: O(1) class Solution { public: long long getDescentPeriods(vector<int>& A) { long long ans = 0, N = A.size(); for (long long i = 0; i < N; ) { long long len = 1; ++i; while (i < N && A[i] == A[i - 1] - 1) ++i, ++len; ans += (1 + len) * len / 2; } return ans; } };
-
class Solution: def getDescentPeriods(self, prices: List[int]) -> int: i, n = 0, len(prices) ans = 0 while i < n: j = i while j + 1 < n and prices[j] - prices[j + 1] == 1: j += 1 t = j - i + 1 ans += t * (t + 1) // 2 i = j + 1 return ans ############ # 2110. Number of Smooth Descent Periods of a Stock # https://leetcode.com/problems/number-of-smooth-descent-periods-of-a-stock/ class Solution: def getDescentPeriods(self, prices: List[int]) -> int: n = len(prices) inc = [1] * n for i in range(1, n): if prices[i - 1] - prices[i] == 1: inc[i] = inc[i - 1] + 1 return sum(inc)
-
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; } }
-
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 }
-
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; }
Discuss
https://leetcode.com/problems/number-of-smooth-descent-periods-of-a-stock/discuss/1635033/C%2B%2B-O(N)-time-one-pass