We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 870c05d commit ee1fb0eCopy full SHA for ee1fb0e
Python/ways-to-split-array-into-three-subarrays.py
@@ -0,0 +1,25 @@
1
+# Time: O(n)
2
+# Space: O(n)
3
+
4
+class Solution(object):
5
+ def waysToSplit(self, nums):
6
+ """
7
+ :type nums: List[int]
8
+ :rtype: int
9
10
+ MOD = 10**9+7
11
12
+ prefix = [0]
13
+ for x in nums:
14
+ prefix.append(prefix[-1]+x)
15
16
+ result = left = right = 0
17
+ for i in xrange(len(nums)):
18
+ left = max(left, i+1)
19
+ while left+1 < len(nums) and prefix[i+1] > prefix[left+1]-prefix[i+1]:
20
+ left += 1
21
+ right = max(right, left)
22
+ while right+1 < len(nums) and prefix[right+1]-prefix[i+1] <= prefix[-1]-prefix[right+1]:
23
+ right += 1
24
+ result = (result + (right-left))%MOD
25
+ return result
0 commit comments