548. Split Array with Equal Sum

2018-01-13  本文已影响0人  matrxyz

Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies following conditions:
0 < i, i + 1 < j, j + 1 < k < n - 1
Sum of subarrays (0, i - 1), (i + 1, j - 1), (j + 1, k - 1) and (k + 1, n - 1) should be equal.
where we define that subarray (L, R) represents a slice of the original array starting from the element indexed L to the element indexed R.

Example:
Input: [1,2,1,2,1,2,1]
Output: True
Explanation:
i = 1, j = 3, k = 5. 
sum(0, i - 1) = sum(0, 0) = 1
sum(i + 1, j - 1) = sum(2, 2) = 1
sum(j + 1, k - 1) = sum(4, 4) = 1
sum(k + 1, n - 1) = sum(6, 6) = 1

Note:
1 <= n <= 2000.
Elements in the given array will be in range [-1,000,000, 1,000,000].

Solution:preSum + HashMap

思路:
三个cuts位置: i, j, k
a (i) b (j) c (k) d 四段
先遍历j可能的位置, 循环中:
再分别遍历i的位置,将sum(a) == sum(b)的可能存在set中,
再遍历k的位置,判断是否有sum(c) == sum(d) 且a、b段也有。
sum计算方式用preSum

Time Complexity: O(N^2) Space Complexity: O(N)

Solution Code:

// a (i) b (j) c (k) d 四段
public class Solution {
    public boolean splitArray(int[] nums) {
        if (nums.length < 7)
            return false;
        int[] sum = new int[nums.length];
        sum[0] = nums[0];
        for (int i = 1; i < nums.length; i++) {
            sum[i] = sum[i - 1] + nums[i];
        }
        
        // 遍历中点j
        for (int j = 3; j < nums.length - 3; j++) {
            HashSet<Integer> set = new HashSet<>();
            for (int i = 1; i < j - 1; i++) {
                // a段 == b段
                if (sum[i - 1] == sum[j - 1] - sum[i]) { 
                    set.add(sum[i - 1]);
                }
            }
            for (int k = j + 2; k < nums.length - 1; k++) {
                // c段 == d段, 且 ab段也都有
                if (sum[nums.length - 1] - sum[k] == sum[k - 1] - sum[j] && set.contains(sum[k - 1] - sum[j])) {
                    return true;
                }
            }
        }
        return false;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读