diff options
Diffstat (limited to 'num_ways_split_arr.py')
-rw-r--r-- | num_ways_split_arr.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/num_ways_split_arr.py b/num_ways_split_arr.py new file mode 100644 index 0000000..28cf758 --- /dev/null +++ b/num_ways_split_arr.py @@ -0,0 +1,33 @@ +# 2270. Number of Ways to Split Array + +""" +you are given a 0-indexed integer array 'nums' of lenght 'n'. 'nums' contains +a valid split at index 'i' if the following are true: the sum of the first 'i ++ 1' elements is greater than or equal to the sum of the last 'n - i - 1' +elements, and there is at least one element to the right of 'i'. return the +number of valid splits in 'nums'. +""" + + +class Solution(object): + def waysToSplitArray(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + end, start = 0, 0 + for i in range(0, len(nums)): + end += nums[i] + ans = 0 + for i in range(0, len(nums) - 1): + start += nums[i] + end -= nums[i] + if start >= end: + ans += 1 + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.waysToSplitArray(nums=[10, 4, -8, 7])) + print(obj.waysToSplitArray(nums=[2, 3, 1, 0])) |