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. allego 输出报告说明

    List of Available Reports Assigned Function Report Lists all assigned functions, sorted by function ...

  2. Hibernate哪点事?

    1.为什么在Hibernate的实体类中要提供一个无参数的构造器这一点非常重要?每个Hibernate实体类必须包含一个 无参数的构造器, 这是因为Hibernate框架要使用Reflection A ...

  3. Eclipse-远程调试服务器代码

    1, 将应用部署到服务器上可以正常启动 2, 在Eclipse中,有相同的项目(程序) 3, 项目右键(ops-web)-->Debug as --> Debug Configuratio ...

  4. 【DDD】领域驱动设计实践 —— UI层实现

    前面几篇blog主要介绍了DDD落地架构及业务建模战术,后续几篇blog会在此基础上,讲解具体的架构实现,通过完整代码demo的形式,更好地将DDD的落地方案呈现出来.本文是架构实现讲解的第一篇,主要 ...

  5. mybatis 架构

    官网地址:http://code.google.com/p/mybatis/ 版本:mybatis 3.2.3 生成工具:mybatis-generator-core-1.3.2-bundle.zip ...

  6. Codeforces 845 A. Chess Tourney 思路:简单逻辑题

    题目: 题意:输入一个整数n,接着输入2*n个数字,代表2*n个选手的实力.    实力值大的选手可以赢实力值小的选手,实力值相同则都有可能赢.    叫你把这2*n个选手分成2个有n个选手的队伍. ...

  7. Button标签自动刷新问题

    在form表单中,button标签在IE浏览器 type类型默认是button ,而在其他浏览器默认是submit. 解决方法1: 设置类型type="button" <bu ...

  8. [原创]KVM虚拟化管理平台的实现

    KVM虚拟化管理平台的实现 源码链接:https://github.com/wsjhk/IaaS_admin.git 根据KVM虚拟化管理的要求,设计并实现网页操作管理KVM虚拟机.设计原理架构如下图 ...

  9. Ionic3学习笔记(五)动画之使用 animate.css

    本文为原创文章,转载请标明出处 目录 前言 animate.css 的使用 animate.scss 的使用 1. 前言 animate.css 是一款强大的.跨浏览器的预设CSS3动画库,内置了很多 ...

  10. 使用wsimport和JAX-WS调用Web Service接口

    本文简单举例说明如何使用wsimport工具和JAX-WS API调用Web Service接口.此方法的优点:使用JDK自带的工具和API接口,无需依赖第三方库. JDK版本:1.8.0_141开发 ...