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. JavaWeb学习之JDBC API中常用的接口和类

    JDBC API中包含四个常用的接口和一个类分别是: 1.Connection接口 2.Statement接口 3.PreparedStatement接口 4.ResultSet接口 5.Driver ...

  2. 升级与修改Nginx

    自从上次安装了Nginx后,学到了很多新的东西,比如http2.0... 而且还发现nginx还出了新版本,遂决定升级下,还是那个URL,下载最新版. ./configure --user=www - ...

  3. Js前端传递json数组至服务器端并解析的实现。

    最近做的一个小项目中需要将json数组数据传递到服务器端进行保存,现分享一下解决思路. 环境:EasyUi+Mvc 4.0 如下: 在上述截图中的红色圈起来的部分,需要在点击保存后通过一次ajax请求 ...

  4. 【BBED】BBED模拟并修复ORA-08102错误

    [BBED]BBED模拟并修复ORA-08102错误 1.1  BLOG文档结构图 1.2  前言部分 1.2.1  导读和注意事项 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其 ...

  5. 利用GPU实现无尽草地的实时渲染

    0x00 前言 在游戏中展现一个写实的田园场景时,草地的渲染是必不可少的,而一提到高效率的渲染草地,很多人都会想起GPU Gems第七章 <Chapter 7. Rendering Countl ...

  6. 移动端与PHP服务端接口通信流程设计(增强版)

    增强地方一: 再增加2张表,一个接口表,一个授权表,设计参考如下: 接口表 字段名 字段类型 注释 api_id int 接口ID api_name varchar(120) 接口名,以"/ ...

  7. GCD之线程挂起与恢复

    我们可以使用dispatch_suspend函数暂停一个queue以阻止它执行block对象;使用dispatch_resume函数继续dispatch queue.调用dispatch_suspen ...

  8. 在Storyboard中为UITableView添加Header和Footer

    我在这里所说的Header和Footer并不是sectionHeader和sectionFooter,而是指UITableView的tableHeaderView和tableFooterView,这两 ...

  9. TCP/IP(二)物理层详解

    前言 在前面说了一下,计算机网络的大概内容,没有去深刻的去了解它,这篇文章给大家分享一下物理层! 我们知道ISO模型是七层,TCP/IP模型是五层,而tcp/ip协议只将七层概括为4层,我们将学习其中 ...

  10. css3换行的三方式的对比(整理)

    CSS3 文本换行 (转载..) 作者 张歆琳 关注 2016.06.20 10:49* 字数 1101 阅读 676评论 1喜欢 6 文本换行其实是个非常常用但并不起眼的特性.你什么都不用设,浏览器 ...