当前位置:网站首页>The simple problem of leetcode is to divide an array into three parts equal to sum
The simple problem of leetcode is to divide an array into three parts equal to sum
2022-06-29 05:54:00 【·Starry Sea】
subject
Give you an array of integers arr, Only can it be divided into three and equal Non empty Return only when partial true, Otherwise return to false.
Formally , If you can find the index i + 1 < j And meet (arr[0] + arr[1] + … + arr[i] == arr[i + 1] + arr[i + 2] + … + arr[j - 1] == arr[j] + arr[j + 1] + … + arr[arr.length - 1]) You can divide the array into three equal parts .
Example 1:
Input :arr = [0,2,1,-6,6,-7,9,1,2,0,1]
Output :true
explain :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
explain :3 + 3 = 6 = 5 - 2 + 2 + 5 + 1 - 9 + 4
Tips :
3 <= arr.length <= 5 * 10^4
-10^4 <= arr[i] <= 10 ^4
source : Power button (LeetCode)
Their thinking
The sum of the three parts is equal , Then the sum of one part is the sum of one third . Sum of traversal array , When there is a situation equal to one third of the sum in the summation process , Then the currently traversed elements can form one or two groups . in addition , Consider the special case of sum of arrays ; Assume that all elements of the array are summed to 0, Then the array may be divided into n Share , Just to prove :sum/3×n=sum. If n≠3 that sum=0.
class Solution:
def canThreePartsEqualSum(self, arr: List[int]) -> bool:
total=sum(arr)
part=total/3
s=0
count=0
for i in arr:
s+=i
if s==part:
count+=1
s=0
return True if count>=3 else False # Suppose there are four parts , Each part is the sum of one-third , Then the sum of the current array must be 0

边栏推荐
- Tcapulusdb Jun · industry news collection (III)
- In 2022, I haven't found a job yet. I have been unemployed for more than one year. What is the "old tester" for eight years?
- What is MES? What does it do?
- 2022 recommended property management industry research report industry development prospect market investment analysis (the attachment is the link to the online disk, and the report is continuously up
- Openfpga wishes you a happy Lantern Festival!
- Use VS to create a static link library Lib and use
- Can use the mouse, will reinstall the computer system tutorial sharing
- The most complete machine learning model training process
- HTTP Caching Protocol practice
- 2022 recommended cloud computing industry research report investment strategy industry development prospect market analysis (the attachment is a link to the online disk, and the report is continuously
猜你喜欢

Robot reinforcement learning - transferring end-to-end videomotor control from simulation to realworld (curl 2017)
![[C language series] - branch and loop statements](/img/bf/656c9189b4ab4387c5acab1c4a2804.jpg)
[C language series] - branch and loop statements

Why Houdini made the pyside2 plug-in crash

2022 recommended property management industry research report industry development prospect market investment analysis (the attachment is the link to the online disk, and the report is continuously up

Analysis report on the investment market of the development planning prospect of the recommended rare earth industry research industry in 2022 (the attachment is a link to the online disk, and the rep

Research Report on the new energy industry of recommended power equipment in 2022 industry development prospect market investment analysis (the attachment is a link to the network disk, and the report

It turns out that the joys and sorrows of programmers are not interlinked

Servlet version conflict causes page 404

Conditional test, if and case conditional test statements of shell script

Rich material libraries make modeling easy and efficient for developers
随机推荐
Use of sed in shell script
How to choose congestion model and anemia model
2022 recommended precious metal industry research report industry development prospect market analysis white paper (the attachment is a link to the online disk, and the report is continuously updated)
Summary of redis basic knowledge points
Creation of Arduino uno development environment
Analysis of ArrayList set in teacher Yang's class
Use VS to create a static link library Lib and use
嵌入式RTOS
After nine years of testing, the salary for interviewing Huawei is 10000. Huawei employees: the company doesn't have such a low salary position
Research Report on recommended specialized, special and new industries in 2022 industry development prospect and market investment analysis (the attachment is a link to the online disk, and the report
Tcapulusdb Jun · industry news collection (VI)
Regular expressions for shell script values
Jenkins operation Chapter 6 mail server sending build results
ICLR is going to have a big discussion on the deep generation model. Max welling and the winner of the AAAI million dollar award are here. Bengio is one of the organizers
Functions and arrays of shell scripts
想问问,券商选哪个比较好尼?本人小白不懂,现在网上开户安全么?
2-nitro-5,10,15,20-tetra (3,5-dimethoxyphenyl) porphyrin (no2tdmpp) H2) /5,10,15,20-tetra (4-methylphenyl) porphyrin (TMPP) H2) Qiyue porphyrin products
QT precautions and RCC download address
2022 recommended cloud computing industry research report investment strategy industry development prospect market analysis (the attachment is a link to the online disk, and the report is continuously
2022 recommended tire industry research report industry development prospect market analysis white paper