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].
 
这道题给了我们一个数组,让我们找出三个位置,使得数组被分为四段,使得每段之和相等,问存不存在这样的三个位置,注意三个位置上的数字不属于任何一段。刚开始博主觉得这题貌似跟之前那道 Partition Equal Subset Sum 很像,所以在想能不能用DP来做,可是想了半天不知道DP该如何定义,更别说推导状态转移方程了。于是就尝试了建立累加和数组,并搜索所有的可能组合,进行暴力破解,结果却TLE了。说明OJ不接受时间复杂度为三次方的解法,那么就要想办法来优化了,博主只好上网学习大神们的解法,发现大神们的解法果然巧妙,只是改变了一个查找顺序,就轻易的将时间复杂度降到了平方级,碉堡了有木有。思路是这样的,因为我们需要找三个位置i,j,k,如果我们按正常的顺序来暴力搜索,那么就会遍历所有的情况,其实大部分的情况都是不符合题意的,会有大量的无用的运算。而如果我们换一个角度,先搜索j的位置,那么i和k的位置就可以固定在一个小的范围内了,而且可以在j的循环里面同时进行,这样就少嵌套了一个循环,所以时间复杂度会降一维度。确定j的范围应该左右各留3个数字,因为四段均不能为空,而且分割位上的数字不能算入四段。再确定了j的位置后,i和k的位置就能分别确定了,我们要做的是先遍历i的所有可能位置,然后遍历所有的拆分情况,如果拆出的两段和相等,则把这个相等的值加入一个集合中,然后再遍历k的所有情况,同样遍历所有的拆分情况,如果拆出两段和相等,再看这个相等的和是否在集合中,如果存在,说明拆出的四段和都可以相同,那么返回true即可,否则当遍历结束了,返回false。唉,为啥自己就想不到呢,估计这就是和大神的差距吧,泪目中。。
 
解法一:
class Solution {
public:
bool splitArray(vector<int>& nums) {
if (nums.size() < ) return false;
int n = nums.size();
vector<int> sums = nums;
for (int i = ; i < n; ++i) {
sums[i] = sums[i - ] + nums[i];
}
for (int j = ; j < n - ; ++j) {
unordered_set<int> s;
for (int i = ; i < j - ; ++i) {
if (sums[i - ] == (sums[j - ] - sums[i])) {
s.insert(sums[i - ]);
}
}
for (int k = j + ; k < n - ; ++k) {
int s3 = sums[k - ] - sums[j], s4 = sums[n - ] - sums[k];
if (s3 == s4 && s.count(s3)) return true;
}
}
return false;
}
};

下面这种解法是递归的暴力破解写法,刚开始博主还纳闷了,为啥博主之前写的迭代形式的暴力破解过不了OJ,而这个递归版本的确能通过呢,仔细研究了一下,发现这种解法有两个地方做了优化。第一个优化是在for循环里面,如果i不等于1,且当前数字和之前数字均为0,那么跳过这个位置,因为加上0也不会对target有任何影响,那为什么要加上i不等于1的判断呢,因为输入数组如果是七个0,那么实际上应该返回true的,而如果没有i != 1这个条件限制,后面的代码均不会得到执行,那么就直接返回false了,是不对的。第二个优化的地方是在递归函数里面,只有当curSum等于target了,才进一步调用递归函数,这样就相当于做了剪枝处理,减少了大量的不必要的运算,这可能就是其可以通过OJ的原因吧。

再来说下子函数for循环的终止条件 i < n - 5 + 2*cnt 是怎么得来的,是的,这块的确是个优化,因为i的位置是题目中三个分割点i,j,k的位置,所以其分别有自己可以取值的范围,由于只有三个分割点,所以cnt的取值可以是0,1,2。
-> 当cnt=0时,说明是第一个分割点,那么i < n - 5,表示后面必须最少要留5个数字,因为分割点本身的数字不记入子数组之和,那么所留的五个数字为:数字,第二个分割点,数字,第三个分割点,数字。
-> 当cnt=1时,说明是第二个分割点,那么i < n - 3,表示后面必须最少要留3个数字,因为分割点本身的数字不记入子数组之和,那么所留的三个数字为:数字,第三个分割点,数字。
-> 当cnt=2时,说明是第三个分割点,那么i < n - 1,表示后面必须最少要留1个数字,因为分割点本身的数字不记入子数组之和,那么所留的一个数字为:数字。

解法二:

class Solution {
public:
bool splitArray(vector<int>& nums) {
if (nums.size() < ) return false;
int n = nums.size(), target = ;
int sum = accumulate(nums.begin(), nums.end(), );
for (int i = ; i < n - ; ++i) {
if (i != && nums[i] == && nums[i - ] == ) continue;
target += nums[i - ];
if (helper(nums, target, sum - target - nums[i], i + , )) {
return true;
}
}
return false;
}
bool helper(vector<int>& nums, int target, int sum, int start, int cnt) {
if (cnt == ) return sum == target;
int curSum = , n = nums.size();
for (int i = start + ; i < n - + * cnt; ++i) {
curSum += nums[i - ];
if (curSum == target && helper(nums, target, sum - curSum - nums[i], i + , cnt + )) {
return true;
}
}
return false;
}
};

基于上面递归的优化方法的启发,博主将两个优化方法加到了之前写的迭代的暴力破解解法上,就能通过OJ了,perfect!

解法三:

class Solution {
public:
bool splitArray(vector<int>& nums) {
int n = nums.size();
vector<int> sums = nums;
for (int i = ; i < n; ++i) {
sums[i] = sums[i - ] + nums[i];
}
for (int i = ; i <= n - ; ++i) {
if (i != && nums[i] == && nums[i - ] == ) continue;
for (int j = i + ; j <= n - ; ++j) {
if (sums[i - ] != (sums[j - ] - sums[i])) continue;
for (int k = j + ; k <= n - ; ++k) {
int sum3 = sums[k - ] - sums[j];
int sum4 = sums[n - ] - sums[k];
if (sum3 == sum4 && sum3 == sums[i - ]) {
return true;
}
}
}
}
return false;
}
};

参考资料:

https://leetcode.com/problems/split-array-with-equal-sum/

https://leetcode.com/problems/split-array-with-equal-sum/discuss/101484/java-solution-dfs

https://leetcode.com/problems/split-array-with-equal-sum/discuss/101481/simple-java-solution-on2

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 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 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 ...

  3. 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 ...

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

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

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

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

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

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

  7. leetcode548 Split Array with Equal Sum

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

  8. 『随笔』.Net 底层 数组[] 的 基本设计探秘 512 子数组

    static void Main(string[] args) { Console.ReadKey(); //初始化数组 不会立即开辟内存字节, 只有实际给数组赋值时 才会开辟内存 // //猜测数组 ...

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

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

随机推荐

  1. [jdoj1817]Drainage Ditches_网络流

    Drainage Ditches jdoj-1817 题目大意:网络流裸求最大流 注释:n(点数),m(边数)<=200. 想法:裸的网络流求最大流,用bfs+dfs,美其名曰dinic. 没有 ...

  2. [日常] 最近的一些破事w...

    更新博文一篇以示诈尸(大雾 (其实只是断了个网然后就彻底失踪了一波w...连题解都没法写了QAQ) $ \tiny{诈尸的实际情况是老姚提前走还把十一机房门锁了然而钥匙在联赛的时候就还了于是并不能进去 ...

  3. beta冲刺6-咸鱼

    前言:此篇是补昨天凌晨的.后面有更新但是太晚了就没有即使更新.所以现在过来更新一下. 昨天的未完成: 用户测试+测试报告 目前剩下的功能点:输入内容检测 我的社团输出显示格式调整. 今天的完成: 我的 ...

  4. 网络1711班 C语言第八次作业批改总结

    网络1711班 C语言第七次作业批改总结 最近在忙一些琐事,没能及时批改大家的作业,连续两次作业总结也没有很用心写,在这要给大家say sorry. 1.本次作业评分细则 1.1 基本要求(1分) 按 ...

  5. C语言--第七周作业

    一.求交错序列前N项和 1.代码 #include <stdio.h> int main() { int i=1,N; double j=0,sum=0; scanf("%d&q ...

  6. Archlinux无线联网教程

    本人是学生党,故对于有线方式不甚了解,学校里一般使用mentohust用动态IP方式联网,或者直接连接wifi,这里介绍无线联网的一些方式,主要包括公共wifi和带有WEP或者WPA或者WPA2PSK ...

  7. Python 科学计算-介绍

    Python 科学计算 作者 J.R. Johansson (robert@riken.jp) http://dml.riken.jp/~rob/ 最新版本的 IPython notebook 课程文 ...

  8. iOS开发-添加圆角效果高效实现

    圆角(RounderCorner)是一种很常见的视图效果,相比于直角,它更加柔和优美,易于接受.但很多人并不清楚如何设置圆角的正确方式和原理.设置圆角会带来一定的性能损耗,如何提高性能是另一个需要重点 ...

  9. Flask学习 三 web表单

    web表单 pip install flask-wtf 实现csrf保护 app.config['SECRET_KEY']='hard to guess string' # 可以用来存储框架,扩展,程 ...

  10. OSI七层协议模型、TCP/IP四层模型学习笔记

    1. OSI七层和TCP/IP四层的关系 1.1 OSI引入了服务.接口.协议.分层的概念,TCP/IP借鉴了OSI的这些概念建立TCP/IP模型. 1.2 OSI先有模型,后有协议,先有标准,后进行 ...