【leetcode】1020. Partition Array Into Three Parts With Equal Sum
题目如下:
Given an array
Aof integers, returntrueif and only if we can partition the array into three non-emptyparts with equal sums.Formally, we can partition the array if we can find indexes
i+1 < jwith(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 + 1Example 2:
Input: [0,2,1,-6,6,7,9,-1,2,0,1]
Output: falseExample 3:
Input: [3,3,6,5,-2,2,5,1,-9,4]
Output: true
Explanation: 3 + 3 = 6 = 5 - 2 + 2 + 5 + 1 - 9 + 4Note:
3 <= A.length <= 50000-10000 <= A[i] <= 10000
解题思路:要使得Input能均分成三段,那么Input所有元素的和一定是3的倍数,这里记为3X。假设下标i和j为第一段和第二段的终点,那么有sum(Input[0:i]) = X,sum(Input[0:j]) = 2X。所以只需要用字典记录整个Input从下标0开始到结束每一段的和,判断和是否能被3整除,以及X和2X是否存在并且X出现在2X之前即可。
代码如下:
class Solution(object):
def canThreePartsEqualSum(self, A):
"""
:type A: List[int]
:rtype: bool
"""
dic = {}
total = 0
for i,v in enumerate(A):
total += v
if total not in dic:
dic[total] = [i]
elif len(dic[total]) == 1:
dic[total] += [i]
else:
dic[total][-1] = i
return total % 3 == 0 and total / 3 in dic and total / 3 * 2 in dic and dic[total/3][0] < dic[total / 3 * 2][-1]
【leetcode】1020. Partition Array Into Three Parts With Equal Sum的更多相关文章
- 【LeetCode】1020. Partition Array Into Three Parts With Equal Sum 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- [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 ...
- 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-e ...
- 【LeetCode】915. Partition Array into Disjoint Intervals 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/partitio ...
- 【leetcode】1043. Partition Array for Maximum Sum
题目如下: Given an integer array A, you partition the array into (contiguous) subarrays of length at mos ...
- 【leetcode】915. Partition Array into Disjoint Intervals
题目如下: 解题思路:题目要求的是在数组中找到一个下标最小的index,使得index左边(包括自己)子序列的最大值小于或者等于右边序列的最小值.那么我们可以先把数组从最左边开始到数组最右边所有子序列 ...
- Leetcode 1013. Partition Array Into Three Parts With Equal Sum
简单题,暴力找出来就行. class Solution: def canThreePartsEqualSum(self, A: List[int]) -> bool: s = sum(A) if ...
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
随机推荐
- 北风设计模式课程---状态模式State(对象行为型)
北风设计模式课程---状态模式State(对象行为型) 一.总结 一句话总结: 状态模式 具体状态的行为在具体的状态类中就解决,不用交给外部做判断.实质是将多条件判断弄成了多个类,在不同的类中做判断 ...
- legend3---1、meedu安装
legend3---1.meedu安装 一.总结 一句话总结: 视频的安装教程做主菜,辅以文档安装教程做配料,口感甚佳. 1.为什么不直接从github下载文件的发行版,而要选择composer方式安 ...
- Step1 - How to: Define a Windows Communication Foundation Service Contract
https://msdn.microsoft.com/en-us/library/ms731835.aspx This is the first of six tasks required to cr ...
- IDEA 服务启动报:No buffer space available (maximum connections reached): connect的解决方案。
错误提示:严重: Error starting endpointjava.io.IOException: Unable to establish loopback connectionat sun.n ...
- toutiao url
https://it.snssdk.com/article/v2/tab_comments/?group_id=6485899113563947533&item_id=648589911356 ...
- loj#500 「LibreOJ β Round」ZQC 的拼图
分析 二分倍数 然后考虑dp[i][j]表示选到第i个x轴覆盖到j的情况y轴最多覆盖多少 贡献柿子可以画图然后相似三角形得到 代码 #include<bits/stdc++.h> usin ...
- EZOJ #362历史
分析 就是保存前pi-1个数每个ai出现多少次 然后维护这些数当前剩余的最大值 每次和新加进来的比较即可 如果新的大直接取 否则新的最大值一定不大于原来的最大值 因此o(n) 代码 #include& ...
- 【CDN+】 Hbase入门 以及Hbase shell基础命令
前言 大数据的基础离不开Hbase, 本文就hbase的基础概念,特点,以及框架进行简介, 实际操作种需要注意hbase shell的使用. Hbase 基础 官网:https://hbase.ap ...
- pycharm5.0.4简易使用说明
前言:学习自动化,需要使用pycharm,以下是简易使用说明 1.注册破解 2.行号和背景色 3.打断点 1.注册破解 打开pycharm5.0.4,点击菜单栏的help->register.. ...
- cron 定时任两种配置方式
第一种:xml文件方式 <bean id="commonTimer" class="com.course.wx.timer.CommonTimer"> ...