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. 基于图形检测API(shape detection API)的人脸检测

    原文:https://paul.kinlan.me/face-detection/ 在 Google 开发者峰会中,谷歌成员 Miguel Casas-Sanchez 跟我说:"嘿 Paul ...

  2. iOS - 内购总结

        如果有人以后要在做内购这一块.希望可以好好的阅读这篇文章,虽然不是字字珠玑.但是也是本人亲人趟过了无数的坑,希望可以对大家有所帮助!  下面是在研究工程中遇到的问题(iOS 内购的流程如下 1 ...

  3. 纳税服务系统【统计图Fusionchart】

    需求 我们在投诉模块中还有一个功能没有实现: 统计:根据年度将相应年度的每个月的投诉数进行统计,并以图表的形式展示在页面中:在页面中可以选择查看当前年度及其前4年的投诉数.在页面中可以选择不同的年度, ...

  4. 关于Linux的虚拟内存管理

    在linux中可以通过free指令查看当前内存,在后面加-m参数能让数字单位显示为MB. 一般机器,有一个实际内存和一个虚拟内存. swap就是虚拟内存,这个虚拟内存可以是文件,也可以是磁盘分区.通常 ...

  5. DI in ASP.NET Core

    .NET-Core Series Server in ASP.NET-Core DI in ASP.NET-Core Routing in ASP.NET-Core Error Handling in ...

  6. JS中关于数组的内容

      前  言 LIUWE 在网站制作过程中,数组可以说是起着举足轻重的地位.今天就给大家介绍一下数组的一些相关内容.例如:如何声明一个数组和在网站制作过程中我们常用的一些数组的方法.介绍的不好还请多多 ...

  7. css样式引入优先级?

    css中的优先级讲的有 1.选择器的优先级. 2.样式引入的优先级. 今天要研究的是样式引入的优先级.网上又很多答案都是如下的,但是真的是这样的吗,让我们来探一探究竟是如何. 四种样式的优先级别是:行 ...

  8. Spring 学习——基于Spring WebSocket 和STOMP实现简单的聊天功能

    本篇主要讲解如何使用Spring websocket 和STOMP搭建一个简单的聊天功能项目,里面使用到的技术,如websocket和STOMP等会简单介绍,不会太深,如果对相关介绍不是很了解的,请自 ...

  9. mysql error 1130 hy000:Host 'localhost' is not allowed to connect to this mysql server 解决方案

    ERROR 1130 (HY000): Host 'localhost' is not allowed to connect to this MySQL server D:\Wamp\mysql-\b ...

  10. PS 软件操作应用处理——粒子化任务效果

      前  言 JRedu 上次分享中,给大家介绍了一些图片的处理方法,主要是通过滤镜里的功能,把图片处理成素描效果或者水彩画效果,营造出不同的氛围. PS是一款非常强大的软件,包含了非常多的功能,合成 ...