1248. Count Number of Nice Subarrays
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it. Return the number of nice sub-arrays.
def numberOfSubarrays2(self, nums, k):
i = count = nice_count = odd_count = 0 # The variable "count" is needed because when "j" hits an even number, it still need to add count
for j in range(len(nums)):
if nums[j] % 2 == 1:
odd_count += 1
count = 0 # Note that when the number is even, count will remain constant
while odd_count == k:
odd_count -= nums[i] % 2
i += 1
count += 1
nice_count += count
return nice_count