FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LeetCode-Solutions/Python/split-array-with-same-average.py at master · tehsints/LeetCode-Solutions · GitHub
tehsints
/
LeetCode-Solutions
Public
forked from
kamyu104/LeetCode-Solutions
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
LeetCode-Solutions
/
Python
/
split-array-with-same-average.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
29 lines (26 loc) · 849 Bytes
Breadcrumbs
LeetCode-Solutions
/
Python
/
split-array-with-same-average.py
Copy path
File metadata and controls
29 lines (26 loc) · 849 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Time: O(n^4)
# Space: O(n^3)
class
Solution
(
object
):
def
splitArraySameAverage
(
self
,
A
):
"""
:type A: List[int]
:rtype: bool
"""
def
possible
(
total
,
n
):
for
i
in
xrange
(
1
,
n
//
2
+
1
):
if
total
*
i
%
n
==
0
:
return
True
return
False
n
,
s
=
len
(
A
),
sum
(
A
)
if
not
possible
(
n
,
s
):
return
False
sums
=
[
set
()
for
_
in
xrange
(
n
//
2
+
1
)]
sums
[
0
].
add
(
0
)
for
num
in
A
:
# O(n) times
for
i
in
reversed
(
xrange
(
1
,
n
//
2
+
1
)):
# O(n) times
for
prev
in
sums
[
i
-
1
]:
# O(1) + O(2) + ... O(n/2) = O(n^2) times
sums
[
i
].
add
(
prev
+
num
)
for
i
in
xrange
(
1
,
n
//
2
+
1
):
if
s
*
i
%
n
==
0
and
s
*
i
//
n
in
sums
[
i
]:
return
True
return
False
Back
|
FazBrowse Home
|
New Git URL