i
and j
to represent the window's left and right ends, respectively.ans
to store the total count of continuous subarrays.i
.
a. Add nums[i]
to the TreeMap and increment its frequency.
b. While the difference between the smallest and largest keys in the TreeMap is greater than 2, increment the left pointer j
and update the TreeMap.
c. Add the length of the current window (i - j + 1
) to ans
.ans
as the total count of continuous subarrays.