Given an array A of integers, return true if and only if we can partition the array into three non-empty parts with equal sums.

Formally, we can partition the array if we can find indexes i+1 < j with (A[0] + A[1] + ... + A[i] == A[i+1] + A[i+2] + ... + A[j-1] == A[j] + A[j-1] + ... + A[A.length - 1])

Example 1:

Input: [0,2,1,-6,6,-7,9,1,2,0,1]
Output: true
Explanation: 0 + 2 + 1 = -6 + 6 - 7 + 9 + 1 = 2 + 0 + 1

Example 2:

Input: [0,2,1,-6,6,7,9,-1,2,0,1]
Output: false

Example 3:

Input: [3,3,6,5,-2,2,5,1,-9,4]
Output: true
Explanation: 3 + 3 = 6 = 5 - 2 + 2 + 5 + 1 - 9 + 4

Note:

  1. 3 <= A.length <= 50000
  2. -10000 <= A[i] <= 10000

Idea 1: S + S +... = 3*S = S + S + (-S) + S + S

Time complexity: O(n), 2 scan

Space complexity: O(1)

 class Solution {
public boolean canThreePartsEqualSum(int[] A) {
int totalSum = 0;
for(int num: A) {
totalSum += num;
} if(totalSum%3 != 0) {
return false;
} int target = totalSum/3; int part = 0;
int sum = 0;
for(int i = 0; i < A.length; ++i) {
sum += A[i];
if(sum == target) {
++part;
if(part >= 2) {
return true;
}
sum = 0;
} } return false;
}
}

Partition Array Into Three Parts With Equal Sum LT1013的更多相关文章

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

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

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

  3. [Swift]LeetCode1013. 将数组分成和相等的三个部分 | 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 non-e ...

  4. 【leetcode】1020. 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 ...

  5. Leetcode 1013. Partition Array Into Three Parts With Equal Sum

    简单题,暴力找出来就行. class Solution: def canThreePartsEqualSum(self, A: List[int]) -> bool: s = sum(A) if ...

  6. 698. Partition to K Equal Sum Subsets

    Given an array of integers nums and a positive integer k, find whether it's possible to divide this ...

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

  8. HDU-3280 Equal Sum Partitions

    http://acm.hdu.edu.cn/showproblem.php?pid=3280 用了简单的枚举. Equal Sum Partitions Time Limit: 2000/1000 M ...

  9. HDU 3280 Equal Sum Partitions(二分查找)

    Equal Sum Partitions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

随机推荐

  1. SQL Server - 使用 Merge 语句实现表数据之间的对比同步

    表数据之间的同步有很多种实现方式,比如删除然后重新 INSERT,或者写一些其它的分支条件判断再加以 INSERT 或者 UPDATE 等.包括在 SSIS Package 中也可以通过 Lookup ...

  2. common mistake of closure in loops

    [common mistake of closure in loops] 下例中item引用的始终是最后一个值. function showHelp(help) { document.getEleme ...

  3. Pandas基本功能之层次化索引及层次化汇总

    层次化索引 层次化也就是在一个轴上拥有多个索引级别 Series的层次化索引 data=Series(np.random.randn(10),index=[ ['a','a','a','b','b', ...

  4. poj2970 The lazy programmer 【优先队列】

    A new web-design studio, called SMART (Simply Masters of ART), employs two people. The first one is ...

  5. NumPy 算术函数

    NumPy 算术函数 NumPy 算术函数包含简单的加减乘除: add(),subtract(),multiply() 和 divide(). 需要注意的是数组必须具有相同的形状或符合数组广播规则. ...

  6. 我和struts2 的第一天

    文字解析 1.获取正文请求 2.根据请求做出判断 3.执行具体的方法代码 4.跳转页面,展示给用户 Strut 是Apache发行的MVC框架 他只是表现层的框架 Strut1 是2005年流行的框架 ...

  7. layer使用

    1引入js <script src="${pageContext.request.contextPath }/js/jquery-1.9.1.min.js" type=&qu ...

  8. 使用命令行执行sql文件

    用Navicat 导入sqlserver数据库时,出现了out of memory异常,百度无果,想起之前用命令行导入过,再次试了一下成功 用法: 打开执行命令: sqlcmd -S localhos ...

  9. 安装sql server 2008 提示错误 SQL Server 2005 Express 工具。 失败

    安装sql server 2008 management,提示错误:Sql2005SsmsExpressFacet 检查是否安装了 SQL Server 2005 Express 工具. 失败,已安装 ...

  10. android 开发概述以及相关背景知识

    参考链接:http://www.runoob.com/android/android-architecture.html http://www.runoob.com/android/android-a ...