aboutsummaryrefslogtreecommitdiff
path: root/ways_split_arr_subarr.py
blob: df65591b961b21c20857abaa61b1dd39962a0c94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 2750. Ways to Split Array Into Good Subarrays

"""
given a binary array 'nums'. a subarray of an array is good if it contains
exactly one element with the value 1. return an integer denoting the number
of ways to split the array 'nums' into good subarrays. as the number may be
too large, return it 10^9+7. a subarray is contiguous non empty sequence of
elements within an array.
"""


class Solution(object):
    def numberOfGoodSubarraySplits(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        ans, lo = 0, -1
        for hi, i in enumerate(nums):
            if i == 1:
                if ans == 0:
                    ans = 1
                else:
                    ans *= hi - lo
                    ans %= 10**9 + 7
                lo = hi
        return ans


if __name__ == "__main__":
    obj = Solution()
    print(obj.numberOfGoodSubarraySplits(nums=[0, 1, 0, 0, 1]))
    print(obj.numberOfGoodSubarraySplits(nums=[0, 1, 0]))