Partition Array Into Three Parts With Equal Sum LT1013
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:
3 <= A.length <= 50000-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的更多相关文章
- 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 ...
- 【LeetCode】1020. Partition Array Into Three Parts With Equal Sum 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [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 ...
- 【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 ...
- Leetcode 1013. Partition Array Into Three Parts With Equal Sum
简单题,暴力找出来就行. class Solution: def canThreePartsEqualSum(self, A: List[int]) -> bool: s = sum(A) if ...
- 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 ...
- [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 ...
- HDU-3280 Equal Sum Partitions
http://acm.hdu.edu.cn/showproblem.php?pid=3280 用了简单的枚举. Equal Sum Partitions Time Limit: 2000/1000 M ...
- HDU 3280 Equal Sum Partitions(二分查找)
Equal Sum Partitions Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
随机推荐
- uuid生成工具类
public class UUIDTool { public static String getUUID() { return UUID.randomUUID().toString().replace ...
- 手机通过Charles抓取https包
因为fiddler不能在mac上使用,而Charles是跨平台的,可以在mac上使用,所以需要了解一下Charles的使用 安装破解版Charles 下载破解版包,先启动一次未破解版的Ch ...
- django实现分页
分页实现思路: 1.接口拉取库内数据,进行分页 2.页面实现分页组件,切换不同链接 一.接口实现: from django.core.paginator import Paginator p=Pagi ...
- HibernateDaoSupport类的使用(转)
看到一篇很好描述HibernateDaoSupport类使用的例子,特此在这和大家分享一下 核心提示:1. 继承了HibernateDaoSupport类的类获取session时,已不可用Sessio ...
- CentOS Netstat命令
语法 netstat(选项) 选项 -a或--all:显示所有连线中的Socket: -A<网络类型>或--<网络类型>:列出该网络类型连线中的相关地址: -c或--conti ...
- Jmeter 录制脚本(二)
1)选择WorkBench,右键 Add -> Non-Test Elements -> HTTP(S) Test Script Recorder 2)在HTTP(S) Test Scri ...
- Mac 查看端口占用及杀死进程
lsof -i : kill -
- python:django
====启动django==== python manager.py runserver --host 0.0.0.0 --port 9008 python manager.py runserver ...
- TZOJ 1937 Hie with the Pie(floyd+状压dp)
描述 The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfo ...
- EasyUI Dialog 对话框 关闭事件
在 $('#×××').dialog('close'); 执行后触发 $(function(){ $("#titledialos").dialog({ onClose: fun ...