当前位置:网站首页>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

边栏推荐
- Le langage C imprime "Love", "Mars hit Earth" et ainsi de suite en utilisant printf, qui est constamment mis à jour
- 證券開戶安全麼,有沒有什麼危險呢
- Test content
- Personal blog item: processing of reading number +1 after viewing article details
- The translation of those exquisite lines in the eighth season of the big bang
- HTTP Caching Protocol practice
- Quickly write MVVM code using source generators
- After nine years of testing, the salary for interviewing Huawei is 10000. Huawei employees: the company doesn't have such a low salary position
- Functions and arrays of shell scripts
- DataX connection MySQL cannot find driver
猜你喜欢

Analysis report on the investment market of the development planning prospect of the recommended wind power industry research industry in 2022 (the attachment is a link to the network disk, and the re
![[chromium] win10 vs2019 environment chromium configuration and compilation.](/img/20/428e6b22ed6955a732dd14d5ff0e3d.jpg)
[chromium] win10 vs2019 environment chromium configuration and compilation.
![ASP. Net core 6 framework unveiling example demonstration [03]:dapr initial experience](/img/fd/4c24e10fc91a7ce7e709a0874ba675.jpg)
ASP. Net core 6 framework unveiling example demonstration [03]:dapr initial experience

What is MES? What does it do?
![[high concurrency] deeply analyze the callable interface](/img/42/43d1f0b894f2632f2c7f1bfe970708.jpg)
[high concurrency] deeply analyze the callable interface

Annual inventory review of Alibaba cloud's observable practices in 2021

机器人强化学习——Transferring End-to-End Visuomotor Control from Simulation to RealWorld (CoRL 2017)

5000+ word interpretation | Product Manager: how to do a good job in component selection?

Jenkins operation Chapter 6 mail server sending build results

Will the order of where conditions in MySQL affect the union index? Will where 1 =1 affect the use of the index? Does where 1 =1 affect the use of indexes?
随机推荐
Hyperledger Fabric 2. X custom smart contract
Implementation of queue
Week 10 - task 1- fill in the blank: line class inherits point class
Use typescript compiler parameter 'skiplibcheck' - usage of the typescript compiler argument'skiplibcheck'
HTTP Caching Protocol practice
ASP. Net core 6 framework unveiling example demonstration [03]:dapr initial experience
Love that can't be met -- what is the intimate relationship maintained by video chat
[C language series] - initial C language (4)
Annual inventory review of Alibaba cloud's observable practices in 2021
Embedded RTOS
Establishing the development environment of esp8266
Difference between parametric continuity and geometric continuity
Research on heuristic intelligent task scheduling
Openfpga wishes you a happy Lantern Festival!
Can redis implement hot standby?
Regular expressions for shell script values
Would like to ask, which is the better choice for securities companies? I don't understand. Is it safe to open an account online now?
嵌入式RTOS
The easiest GUI to compile: dgui
STI, one controller