Minimum Recolors to Get K Consecutive Black Blocks (Leetcode 2379)

Problem Link: https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/

class Solution:
    def numberOfSubarrays(self, nums: List[int], k: int) -> int:
        
        ans=0
        c=0
        j=0
        odd=0
        n=len(nums)
        
        for i in range(n):
            if(nums[i]%2!=0):
                odd+=1
                if(odd>=k):
                    c=1
                    while(nums[j]%2==0):
                        c+=1
                        j+=1
                    j+=1
                    ans=ans+c
            
            else:
                if(odd>=k):
                    ans=ans+c
        
        return ans           

Last updated