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

 class Solution
{
public boolean splitArray(int[] nums)
{
if(nums.length < 7) // at least need 7 numbers
return false; int[] sum = new int[nums.length];
sum[0] = nums[0];
// create sum array: each sum has sum from 1 to i
for(int i=1; i<nums.length; i++)
sum[i] = sum[i-1] + nums[i]; // for j - middle cut
for(int j=3; j<nums.length-3; j++)
{
HashSet<Integer> set = new HashSet<>();
// for i - left cut
for(int i=1; i<j-1; i++)
{
int sum1 = sum[i-1];
int sum2 = sum[j-1] - sum[i];
if(sum1 == sum2)
set.add(sum1); // add potential answers into set
}
// for k - right cut
for(int k=j+2; k<nums.length-1; k++)
{
int sum3 = sum[k-1] - sum[j];
int sum4 = sum[nums.length - 1] - sum[k];
if( sum3 == sum4 && set.contains(sum3)) //
return true;
} } return false;
}
}

参考资料:

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

LeetCode 题目列表 - LeetCode Questions List

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

  1. [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 fol ...

  2. [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 fol ...

  3. 【LeetCode】548. Split Array with Equal Sum 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 日期 题目地址:https://leetcode ...

  4. [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 ini ...

  5. 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...

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

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  7. [LeetCode] Split Array into Fibonacci Sequence 分割数组成斐波那契序列

    Given a string S of digits, such as S = "123456579", we can split it into a Fibonacci-like ...

  8. leetcode548 Split Array with Equal Sum

    思路: 使用哈希表降低复杂度.具体来说: 枚举j: 枚举i,如果sum[i - 1] == sum[j - 1] - sum[i],就用哈希表把sum[i - 1]记录下来: 枚举k,如果sum[k ...

  9. [LeetCode] 915. Partition Array into Disjoint Intervals 分割数组为不相交的区间

    Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...

随机推荐

  1. Eclipse: eclipse文本文件编码格式更改(GBK——UTF-8)

    Eclipse中设置编码的方式 Eclipse工 作空间(workspace)的缺省字符编码是操作系统缺省的编码,简体中文操作系统 (Windows XP.Windows 2000简体中文)的缺省编码 ...

  2. Spring4 customEditors

    Spring4.0版本以后customEditors属性为Map<Class<?>, Class<? extends PropertyEditor>>,所以用key ...

  3. 使用Gateway-Worker实现多人分组实时聊天 结合第三方tp

    一.基础知识1.Workerman是一款纯PHP开发的开源高性能的PHP socket 服务器框架.被广泛的用于手机app.移动通讯等领域的开发. 支持TCP长连接,支持Websocket.HTTP等 ...

  4. NDK调试

    第一种(控制台输出): 1.配置好环境变量,这是为了方便起见.将你sdk和ndk的根目录放到环境变量path中.配置完成之后可以来个小检测: 在命令行分别输入adb和ndk-stack后点击回车,只要 ...

  5. 编号中的数学_KEY

    题目描述: 从美国州际高速公路建筑者那里,奶牛们引进了一种路径编号系统,来给牧场之间的道 路编号.他们已经把 N(1<=N<=25)个牧场,用 1 到 N 的整数编号.现在他们需要将牧场间 ...

  6. JavaScript遍历对象-总结一

    原生JavaScript 遍历 1.for 循环遍历 let array1 = ['a','b','c']; for (let i = 0;i < array1.length;i++){ con ...

  7. 约会安排HDU - 4553

    寒假来了,又到了小明和女神们约会的季节.  小明虽为屌丝级码农,但非常活跃,女神们常常在小明网上的大段发言后热情回复"呵呵",所以,小明的最爱就是和女神们约会.与此同时,也有很多基 ...

  8. 五年.net程序员转型Java之路

    大学毕业后笔者进入一家外企,做企业CRM系统开发,那时候开发效率最高的高级程序语言,毫无疑问是C#.恰逢公司也在扩张,招聘了不少.net程序员,笔者作为应届生,也乐呵呵的加入到.net程序员行列中. ...

  9. 【京东个人中心】——Nodejs/Ajax/HTML5/Mysql爬坑之注册与登录监听

    一.引言 在数据库和静态页面都创建好之后,下面就该接着完成后台Node.js监听注册和登录的部分了.这个部分主要使用的技术是:Node.js的Express框架和ajax异步请求.登录和注册的代码实现 ...

  10. Windows开启telnet服务 + 连接失败处理

    一.控制面板中安装Telnet相关组件 单击"开始"菜单,单击"控制面板"     在控制面板中单击打开"程序和功能"项目   在左侧的蓝色 ...