Leetcode 1370 Solution
This article provides solution to leetcode question 1370 (count-number-of-nice-subarrays)
Access this page by simply typing in "lcs 1370" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/count-number-of-nice-subarrays
Solution
class Solution:
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
if not nums:
return 0
m = [0] * (len(nums) + 1)
m[0] = 1
odd_cnt = 0
ans = 0
for num in nums:
odd_cnt += num % 2
if odd_cnt >= k and m[odd_cnt - k] > 0:
ans += m[odd_cnt - k]
m[odd_cnt] += 1
return ans