Straight forward python solution


  • 0
    Z
    1. The first for loop creates array sum in ascending order, so that we have a way to query range array sum using constant time.

    2. Then we are looking for a range 0 ~ i-1 and k+1 ~ n-1 which have equal range sum.

    3. Finally I am looking for a splitter j that splits the range between i + 1 and k - 1 so that the sum range i+1 ~ j-1 and j+1 ~ k-1 are equal, we also need to check if this sum range equal to either 0 ~ i-1 or k+1 ~ n-1. Then we are done.

    class Solution(object):
        def splitArray(self, nums):
            for i in xrange(1, len(nums)):
                nums[i] += nums[i - 1]
            for i in xrange(len(nums) - 6):
                for k in xrange(i + 6, len(nums)):
                    if nums[i] == nums[-1] - nums[k - 1]:
                        for j in xrange(i + 2, k - 1):
                            if nums[j - 1] - nums[i + 1] == nums[k - 2] - nums[j] == nums[i]:
                                return True
            return False

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.