2750. Ways to Split Array Into Good Subarrays
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 34% Topics: Array, Math, Dynamic Programming
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
# combinatorics
class Solution(object):
def numberOfGoodSubarraySplits(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
MOD = 10**9+7
result, prev = 1, -1
for i in xrange(len(nums)):
if nums[i] != 1:
continue
if prev != -1:
result = (result*(i-prev))%MOD
prev = i
return result if prev != -1 else 0
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions