LeetCode 548. Split Array with Equal Sum (分割数组使得子数组的和都相同)$

Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies following conditions:

  1. 0 < i, i + 1 < j, j + 1 < k < n - 1
  2. 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. 1 <= n <= 2000.
  2. Elements in the given array will be in range [-1,000,000, 1,000,000].


题目标签:Array

  题目给了我们一个nums array,要我们利用三条分割线 i, j, k 来分割数组成为4 部分,每一个部分的sum 都相同。i j k 都有各自的边界。

  时间复杂度O(n*n)的方法比较巧妙,改变一下搜索的先后顺序,并且结合利用HashSet 就可以把n^3 将为 n^2 时间。

  首先建立一个累加的sum array 来记录每一个num 在nums 里的和(从0到num),便于在后面搜索的时候方便利用。

  遍历中间的分割线 j 的范围(从左到右):

    当分割线 j 确定了之后,分别遍历 分割线 i 和 k 在适当的范围内;

    遍历分割线 i 的范围,从左边到j:找出 sum1(i左边)和 sum2(i右边)相等的所有情况,加入set。(sum1 和sum2 相等的话,有可能是正确答案)

    遍历分割线 k 的范围,从j 到右边: 找出 sum3(k左边)和sum4(k右边)相等的所有情况,每遇到一次sum3 == sum4 的情况,就去set 里找 sum3 的值 是否出现过,有的话说明 sum1 = sum2 = sum3 = sum4, 找到答案直接返回。

Java Solution:

Runtime beats 74.91%

完成日期:09/26/2017

关键词:Array, HashSet

关键点:搜索顺序为 确定中线j,再去找i 和k 结合 HashSet 来确定sum1 = sum2 = sum3 = sum4

 1 class Solution
 2 {
 3     public boolean splitArray(int[] nums)
 4     {
 5         if(nums.length < 7) // at least need 7 numbers
 6             return false;
 7
 8         int[] sum = new int[nums.length];
 9         sum[0] = nums[0];
10         // create sum array: each sum has sum from 1 to i
11         for(int i=1; i<nums.length; i++)
12             sum[i] = sum[i-1] + nums[i];
13
14         // for j - middle cut
15         for(int j=3; j<nums.length-3; j++)
16         {
17             HashSet<Integer> set = new HashSet<>();
18             // for i - left cut
19             for(int i=1; i<j-1; i++)
20             {
21                 int sum1 = sum[i-1];
22                 int sum2 = sum[j-1] - sum[i];
23                 if(sum1 == sum2)
24                     set.add(sum1); // add potential answers into set
25             }
26             // for k - right cut
27             for(int k=j+2; k<nums.length-1; k++)
28             {
29                 int sum3 = sum[k-1] - sum[j];
30                 int sum4 = sum[nums.length - 1] - sum[k];
31                 if( sum3 == sum4 && set.contains(sum3)) //
32                     return true;
33             }
34
35         }
36
37         return false;
38     }
39 }

参考资料:

https://discuss.leetcode.com/topic/85026/simple-java-solution-o-n-2

LeetCode 题目列表 - LeetCode Questions List

时间: 2024-07-30 06:51:58

LeetCode 548. Split Array with Equal Sum (分割数组使得子数组的和都相同)$的相关文章

[LeetCode] Split Array with Equal Sum 分割数组成和相同的子数组

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

[LeetCode] Split Array With Same Average 分割数组成相同平均值的小数组

In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and

[LeetCode] 659. Split Array into Consecutive Subsequences 将数组分割成连续子序列

You are given an integer array sorted in ascending order (may contain duplicates), you need to split them into several subsequences, where each subsequences consist of at least 3 consecutive integers. Return whether you can make such a split. Example

[LeetCode] Split Array Largest Sum 分割数组的最大值

Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays. Note:Given m satisfies the following const

Leetcode 410. Split Array Largest Sum

Problem: Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays. Note: If n is the length of array

[LeetCode] Split Array into Consecutive Subsequences 将数组分割成连续子序列

You are given an integer array sorted in ascending order (may contain duplicates), you need to split them into several subsequences, where each subsequences consist of at least 3 consecutive integers. Return whether you can make such a split. Example

[LeetCode] 805. Split Array With Same Average 用相同均值拆分数组

In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and

leetcode 659. Split Array into Consecutive Subsequences

You are given an integer array sorted in ascending order (may contain duplicates), you need to split them into several subsequences, where each subsequences consist of at least 3 consecutive integers. Return whether you can make such a split. Example

LeetCode 842. Split Array into Fibonacci Sequence

原题链接在这里:https://leetcode.com/problems/split-array-into-fibonacci-sequence/ 题目: Given a string S of digits, such as S = "123456579", we can split it into a Fibonacci-like sequence [123, 456, 579]. Formally, a Fibonacci-like sequence is a list F o