2110. Number of Smooth Descent Periods of a Stock

T: O(n)

S: O(1)

class Solution {
    public long getDescentPeriods(int[] prices) {
        int n = prices.length;
        int start = 0;
        long count = 1;
        for (int end = 1; end < n; end++) {
            if (prices[end-1] - prices[end] == 1) {
                count += end - start + 1;
            } else {
                start = end;
                count++;
            }
        }
        return count;
    }
}

/*


2 3
   end
[3,2,1,4]
       s
*/

Last updated