Number of Smooth Descent Periods of a Stock LeetCode solution in Java
1class Solution {2public long getDescentPeriods(int[] prices) {3int i = 0;4int j = 1;5long ans = 1;6while (j < prices.length) {7if (prices[j - 1] - prices[j] == 1) {8// It means that j(current element) can be part of previous subarrays (j-i)9// and can also start a subarray from me (+1). So add (j-i+1) in total Subarrays10int count = j - i + 1;11ans += count;12} else {13// It means that j cannot be part of previous subarrays but can start subarray from me. So,14// ans+=115i = j;16ans += 1;17}18j++;19}20return ans;21}22}