[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 following conditions:
- 0 < i, i + 1 < j, j + 1 < k < n - 1
- 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 <= n <= 2000.
- Elements in the given array will be in range [-1,000,000, 1,000,000].
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] Split Array with Equal Sum 分割数组成和相同的子数组的更多相关文章
- [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 ...
- [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 ...
- 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 ...
- [LeetCode] Split Array into Fibonacci Sequence 分割数组成斐波那契序列
Given a string S of digits, such as S = "123456579", we can split it into a Fibonacci-like ...
- 【LeetCode】548. Split Array with Equal Sum 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 日期 题目地址:https://leetcode ...
- 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...
- leetcode548 Split Array with Equal Sum
思路: 使用哈希表降低复杂度.具体来说: 枚举j: 枚举i,如果sum[i - 1] == sum[j - 1] - sum[i],就用哈希表把sum[i - 1]记录下来: 枚举k,如果sum[k ...
- 『随笔』.Net 底层 数组[] 的 基本设计探秘 512 子数组
static void Main(string[] args) { Console.ReadKey(); //初始化数组 不会立即开辟内存字节, 只有实际给数组赋值时 才会开辟内存 // //猜测数组 ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
随机推荐
- Mysql的执行计划各个参数详细说明
执行计划各个参数的说明 1.id 主要是用来标识sql的执行顺序,如果没有子查询,一般来说id只有一个,执行顺序也是从上到下 2.select_type 每个select子句的类型 a: simpl ...
- hibernate的一级和二级缓存
一级缓存就是Session级别的缓存,close后就没了. 二级缓存就是SessionFactory级别的缓存,全局缓存,要配置其他插件. 什么样的数据适合存放到第二级缓存中? 1.很少被修改的数据 ...
- Git中一些远程库操作的细节
最近在公司,老是遇到Git远程操作的问题,现总结如下: 1,本地checkout一个新的分支,向远程push的时候,若远程没有该分支,会新建一个. 2.将远程代码clone到本地修改并commit后, ...
- 关于php日期前置是否有0
例如:2018-01-04,这个日期和月份前置是有0 如果不想有0,date( 'y-n-j',time() ):默认的是date( 'y-m-d',time() ),这个日期和月份前置是有0. da ...
- JQuery :contains选择器,可做搜索功能,搜索包含关键字的dom
假设有一个加油站列表,找到所有包含某某关键字的加油站. 选择所有包含 "is" 的 <p> 元素: $("p:contains(is)") 搜索功能 ...
- 2017-2018-1 Java演绎法 小组会议及交互汇总
第一周会议 今天我们小组开展了第一次团队例会活动.我们小组将<构建之法>分为了六个部分并由六位成员先分别学习并向组长上传学习收获,这次的活动内容便是 交流前两周小组成员学习阅读<构建 ...
- equalsignorecase 和equals的区别
equals方法来自于Object类equalsIgnoreCase方法来自String类equals对象参数是Object 用于比较两个对象是否相等equals在Object类中方法默然比较对象内存 ...
- 服务器数据恢复_Linux网站服务器故障数据恢复案例
[数据恢复故障描述] 一台linux网站服务器,DELL R200,管理约50个左右网站,使用一块SATA 160GB硬盘.正常使用中突然宕机,尝试再次启动失败,将硬盘拆下检测时发现存在约100个坏扇 ...
- Python内置函数(22)——list
英文文档: class list([iterable]) Rather than being a function, list is actually a mutable sequence type, ...
- ELK学习总结(2-4)bulk 批量操作-实现多个文档的创建、索引、更新和删除
bulk 批量操作-实现多个文档的创建.索引.更新和删除 ----------------------------------------------------------------------- ...