2031. Count Subarrays With More Ones Than Zeros

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2031

from sortedcontainers import SortedList


class Solution:
    def subarraysWithMoreZerosThanOnes(self, nums: List[int]) -> int:
        sl = SortedList([0])
        mod = 10**9 + 7
        res = s = 0
        for x in nums:
            s += x or -1
            res += sl.bisect_left(s)
            res %= mod
            sl.add(s)
        return res