题目地址:https://leetcode-cn.com/problems/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:

  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 <= n <= 2000.
  • Elements in the given array will be in range [-1,000,000, 1,000,000].

题目大意

在nums数组中插入三个隔板i,j,k,使得 (0, i - 1), (i + 1, j - 1), (j + 1, k - 1) and (k + 1, n - 1)的和相等。

解题方法

暴力

这个题好像没有简单的做法,直接暴力三层循环即可。一个常见的优化就是提前算出从位置0到每个位置的累加和preSum,这样区间[i, j]的和 = preSum[j] - preSum[i - 1];

另外有个case是1000多个0,导致超时,此时需要一个优化:跳过nums[j] == nums[j-1] == 0的点。

C++代码如下:

class Solution {
public:
bool splitArray(vector<int>& nums) {
const int N = nums.size();
vector<long long> preSum(N, 0);
long long sum = 0;
for (int i = 0; i < N; ++i) {
sum += nums[i];
preSum[i] = sum;
}
for (int i = 1; i < N; ++i) {
long long sum_0i = preSum[i - 1];
for (int j = i + 1; j < N; ++j) {
long long sum_ij = preSum[j - 1] - preSum[i];
if ((nums[j] == 0 && nums[j - 1] == 0) || sum_0i != sum_ij)
continue;
for (int k = j + 1; k < N; ++k) {
long long sum_jk = preSum[k - 1] - preSum[j];
if (sum_0i != sum_jk)
continue;
long long sum_k = preSum[N - 1] - preSum[k];
if (sum_0i == sum_k) {
return true;
}
}
}
}
return false;
}
};

日期

2019 年 9 月 20 日 —— 是选择中国互联网式加班?还是外企式养生?

【LeetCode】548. Split Array with Equal Sum 解题报告(C++)的更多相关文章

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

  3. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  4. 【LeetCode】1020. Partition Array Into Three Parts With Equal Sum 解题报告(Python)

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

  5. [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 ...

  6. LeetCode 1013 Partition Array Into Three Parts With Equal Sum 解题报告

    题目要求 Given an array A of integers, return true if and only if we can partition the array into three  ...

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

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

  8. 【LeetCode】842. Split Array into Fibonacci Sequence 解题报告(Python & C++)

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

  9. leetcode548 Split Array with Equal Sum

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

随机推荐

  1. 使用SpringBoot实现登录注册的几个问题

    一.用户名密码都正确的情况下被登录拦截器拦截 控制台报错:org.apache.ibatis.executor.ExecutorException: A query was run and no Re ...

  2. Java日期时间操作基础——包含JDK1.8时间操作新特性

    JDK1.7日期时间操作 示例小结 public class DateTest { public static final String FORMAT_DATE = "yyyy-MM-dd& ...

  3. python-3.x- 序列操作

    1. list操作 A.添加元素 1 list = ["C++","C", "Java", "Python"] 2 &q ...

  4. API 管理在云原生场景下的机遇与挑战

    作者 | 张添翼 来源 | 尔达Erda公众号 ​ 云原生下的机遇和挑战 标准和生态的意义 自从 Kubernetes v1.0 于 2015 年 7 月 21 日发布,CNCF 组织随后建立以来,其 ...

  5. Go Robot

    1 <html> 2 <meta http-equiv="Content-Type" content="text/html; charset=utf-8 ...

  6. ceph对象存储场景

    安装ceph-radosgw [root@ceph-node1 ~]# cd /etc/ceph # 这里要注意ceph的源,要和之前安装的ceph集群同一个版本 [root@ceph-node1 c ...

  7. Flink(九)【Flink的重启策略】

    目录 1.Flink的重启策略 2.重启策略 2.1未开启checkpoint 2.2开启checkpoint 1)不设置重启策略 2)不重启 3)固定延迟重启(默认) 4)失败率重启 3.重启效果演 ...

  8. Oracle——创建存储过程

    有个超级详细的关于存储过程的帖子:https://www.cnblogs.com/snowballed/p/6766867.html Oracle-存储过程(procedure.function.pa ...

  9. Output of C++ Program | Set 3

    Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 using namespace st ...

  10. RHEL 6.5 安装ORACEL11gR2

    1.关闭selinux,用vi /etc/selinux/config selinux=disabled 2.使用yum安装rpm yum -y install compat-db compat-db ...