首页 > 其他分享 >LeetCode 1013. Partition Array Into Three Parts With Equal Sum

LeetCode 1013. Partition Array Into Three Parts With Equal Sum

时间:2024-07-01 12:09:36浏览次数:1  
标签:count arr int curSum Sum Partition sum array Into

原题链接在这里:https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/description/

题目:

Given an array of integers arr, return true 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 (arr[0] + arr[1] + ... + arr[i] == arr[i + 1] + arr[i + 2] + ... + arr[j - 1] == arr[j] + arr[j + 1] + ... + arr[arr.length - 1])

Example 1:

Input: arr = [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: arr = [0,2,1,-6,6,7,9,-1,2,0,1]
Output: false

Example 3:

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

Constraints:

  • 3 <= arr.length <= 5 * 104
  • -104 <= arr[i] <= 104

题解:

Find out if the array could be dividied into three subarrays with equal sum.

Get the sum of array, if it is not divisable by 3, then it can't be divided into 3 equal sum subarrays, return false.

Otherwise, whenever we hit sum / 3, we accumlate the count by plus one.

Eventually check if the count is >= 3. 

Reason why count >= 3, there could be 0, 0, 0, 0. It could still be divided into 3 groups, with one group having 2 0s.

At the end, it should not check if curSum == 0 since there could be case that [-1, -1, -1, -1, -1, 2], when count = 5 and curSum = 2.

Time Complexity: O(n). Space: O(1).

AC Java:

 1 class Solution {
 2     public boolean canThreePartsEqualSum(int[] arr) {
 3         if(arr == null || arr.length == 0){
 4             return false;
 5         }
 6 
 7         int sum = 0;
 8         for(int num : arr){
 9             sum += num;
10         }
11 
12         if(sum % 3 != 0){
13             return false;
14         }
15 
16         int count = 0;
17         int target = sum / 3;
18         int curSum = 0;
19         for(int num : arr){
20             curSum += num;
21             if(curSum == target){
22                 count++;
23                 curSum = 0;
24             }
25         }
26 
27         return count >= 3;
28     }
29 }

 

标签:count,arr,int,curSum,Sum,Partition,sum,array,Into
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18277786

相关文章

  • [题解]CF1712E1 LCM Sum (easy version)
    思路这是一道极好的思维题,主要考察了:组合数学和正难则反的方法。这题可以发现如果用直接法将十分的繁琐,于是乎,我们可以用正难则反的方法,即:总的减去不满足的。这道题总的很好求,为:\(C_{r-l+1}^{3}\)。不满足的情况,我们就可以将题目转化为:\(\operatorname{lcm}(i,j,k)<i+......
  • fdisk时WARNING: Re-reading the partition table failed with error 16: 设备或资源
    WARNING:Re-readingthepartitiontablefailedwitherror16:设备或资源现象:划分磁盘有警告, WARNING:Re-readingthepartitiontablefailedwitherror16:设备或资源忙.Thekernelstillusestheoldtable.Thenewtablewillbeusedatthenextrebootoraft......
  • [题解]CF1066E Binary Numbers AND Sum
    思路考虑对于每一个\(a\)上数位进行分析。令\(a_i\)表示\(a\)在二进制表示中从左往右数的第\(i\)位上的数字,\(b_i\)同理。分类讨论一下\(a_i\)的取值对于答案的贡献:如果\(a_i=0\),对于这一位无论如何都不会拥有贡献。如果\(a_i=1\),因为\(b\)会向右移,所以能......
  • Java8 Consumer、Supplier、Predicate、Function
    今天我们还讲讲Consumer、Supplier、Predicate、Function这几个接口的用法,在Java8的用法当中,这几个接口虽然没有明目张胆的使用,但是,却是润物细无声的。为什么这么说呢?这几个接口都在java.util.function包下的,分别是Consumer(消费型)、supplier(供给型)、predicate(谓词型)、functi......
  • 【Linux详解】冯诺依曼架构 | 操作系统设计 | 斯坦福经典项目Pintos
    目录一.冯诺依曼体系结构(VonNeumannArchitecture)注意事项存储器的意义:缓冲数据流动示例二.操作系统(OperatingSystem)操作系统的概念操作系统的定位与目的操作系统的管理系统调用和库函数操作系统的管理:sum三.系统调用实现示例:Pintos项目Step1:进入ex......
  • [题解]CF622F The Sum of the k-th Powers
    思路首先发现\(\sum_{i=1}^{n}i^k\)是一个\(k+1\)次多项式,那么我们需要求出\(k+2\)个点才能得到唯一的一个\(f(t)=\sum_{i=1}^{t}{i^k}\)。不难通过拉格朗日插值法,将\(x=1\sim(k+2)\)的情况一一带入:\[f(n)=\sum_{i=1}^{k+2}{((\sum_{j=1}^{i}......
  • UNIQUE VISION Programming Contest 2024 Summer (AtCoder Beginner Contest 359)
    A-CountTakahashi(abc359A)解题思路遍历判断即可神奇的代码#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<map>#include<set>#include<cstring>usingnamespacestd;#defineendl'\n......
  • 精仿微信UI应用,基于SumerUI 3.0和Uniapp前端框架的一款仿微信APP应用,界面漂亮颜值高,视
    sumer-weixin介绍精仿微信UI应用,基于SumerUI3.0和Uniapp前端框架的一款仿微信APP应用,界面漂亮颜值高,视频商城小工具等,朋友圈视频号即时聊天用于视频,商城,直播,聊天,等等场景,源码分享源码说明:本源码包只提供1.0版本,只有1.0版本是开源的,提供给大家学习研究。源码使用Hbui......
  • ABC359 G - Sum of Tree Distance
    题目链接题目大意给出一棵树,树上每个节点都有一种颜色,求所有颜色相同的节点两两之间距离的总和。 题解想来写题解主要是看了一下官方解法都写的需要“重心分解”,应该是对应中文语境下的树的点分治。实际上点分治写起来很费事,可以用启发式合并替代。具体来说,dfs时每个节点......
  • UNIQUE VISION Programming Contest 2024 Summer (AtCoder Beginner Contest 359) 题
    点我看题A-CountTakahashi没啥好说的点击查看代码#include<bits/stdc++.h>#definerep(i,n)for(inti=0;i<n;++i)#definerepn(i,n)for(inti=1;i<=n;++i)#defineLLlonglong#definefifirst#definesesecond#definepbpush_back#definemprmake_pair......