Continuous Subarrays

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution:
    def continuousSubarrays(self, nums: List[int]) -> int:
        start_index = total_count = 0
        last_indexes = dict()
        for current_index, current_num in enumerate(nums):
            temp_dict = last_indexes.copy()
            for number, last_index in temp_dict.items():
                if abs(number - current_num) > 2:
                    start_index = max(start_index, last_index + 1)
                    last_indexes.pop(number)
            last_indexes[current_num] = current_index
            total_count += current_index - start_index + 1
        return total_count