Number of Smooth Descent Periods of a Stock LeetCode solution in Java

1
class Solution {
2
public long getDescentPeriods(int[] prices) {
3
int i = 0;
4
int j = 1;
5
long ans = 1;
6
while (j < prices.length) {
7
if (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 Subarrays
10
int count = j - i + 1;
11
ans += count;
12
} else {
13
// It means that j cannot be part of previous subarrays but can start subarray from me. So,
14
// ans+=1
15
i = j;
16
ans += 1;
17
}
18
j++;
19
}
20
return ans;
21
}
22
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0