Skip to content

Commit a8341b2

Browse files
committed
+ problem 805
1 parent 07314e8 commit a8341b2

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# 805. Split Array With Same Average
2+
You are given an integer array `nums`.
3+
4+
You should move each element of `nums` into one of the two arrays `A` and `B` such that `A` and `B` are non-empty, and `average(A) == average(B)`.
5+
6+
Return `true` if it is possible to achieve that and `false` otherwise.
7+
8+
**Note** that for an array `arr`, `average(arr)` is the sum of all the elements of `arr` over the length of `arr`.
9+
10+
#### Example 1:
11+
<pre>
12+
<strong>Input:</strong> nums = [1,2,3,4,5,6,7,8]
13+
<strong>Output:</strong> true
14+
<strong>Explanation:</strong> We can split the array into [1,4,5,8] and [2,3,6,7], and both of them have an average of 4.5.
15+
</pre>
16+
17+
#### Example 2:
18+
<pre>
19+
<strong>Input:</strong> nums = [3,1]
20+
<strong>Output:</strong> false
21+
</pre>
22+
23+
#### Constraints:
24+
* `1 <= nums.length <= 30`
25+
* <code>0 <= nums[i] <= 10<sup>4</sup></code>
26+
27+
## Solutions (Python)
28+
29+
### 1. Solution
30+
```Python
31+
class Solution:
32+
def splitArraySameAverage(self, nums: List[int]) -> bool:
33+
if len(nums) < 2:
34+
return False
35+
36+
s = sum(nums)
37+
sums = [set() for _ in range(len(nums) // 2 + 1)]
38+
sums[0].add(0)
39+
40+
for num in nums:
41+
for i in range(len(sums) - 2, -1, -1):
42+
for x in sums[i]:
43+
if (x + num) * len(nums) == s * (i + 1):
44+
return True
45+
46+
sums[i + 1].add(x + num)
47+
48+
return False
49+
```
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# 805. 数组的均值分割
2+
给定你一个整数数组 `nums`
3+
4+
我们要将 `nums` 数组中的每个元素移动到 `A` 数组 或者 `B` 数组中,使得 `A` 数组和 `B` 数组不为空,并且 `average(A) == average(B)`
5+
6+
如果可以完成则返回`true` , 否则返回 `false`
7+
8+
**注意:**对于数组 `arr` , `average(arr)``arr` 的所有元素的和除以 `arr` 长度。
9+
10+
#### 示例 1:
11+
<pre>
12+
<strong>输入:</strong> nums = [1,2,3,4,5,6,7,8]
13+
<strong>输出:</strong> true
14+
<strong>解释:</strong> 我们可以将数组分割为 [1,4,5,8] 和 [2,3,6,7], 他们的平均值都是4.5。
15+
</pre>
16+
17+
#### 示例 2:
18+
<pre>
19+
<strong>输入:</strong> nums = [3,1]
20+
<strong>输出:</strong> false
21+
</pre>
22+
23+
#### 提示:
24+
* `1 <= nums.length <= 30`
25+
* <code>0 <= nums[i] <= 10<sup>4</sup></code>
26+
27+
## 题解 (Python)
28+
29+
### 1. 题解
30+
```Python
31+
class Solution:
32+
def splitArraySameAverage(self, nums: List[int]) -> bool:
33+
if len(nums) < 2:
34+
return False
35+
36+
s = sum(nums)
37+
sums = [set() for _ in range(len(nums) // 2 + 1)]
38+
sums[0].add(0)
39+
40+
for num in nums:
41+
for i in range(len(sums) - 2, -1, -1):
42+
for x in sums[i]:
43+
if (x + num) * len(nums) == s * (i + 1):
44+
return True
45+
46+
sums[i + 1].add(x + num)
47+
48+
return False
49+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def splitArraySameAverage(self, nums: List[int]) -> bool:
3+
if len(nums) < 2:
4+
return False
5+
6+
s = sum(nums)
7+
sums = [set() for _ in range(len(nums) // 2 + 1)]
8+
sums[0].add(0)
9+
10+
for num in nums:
11+
for i in range(len(sums) - 2, -1, -1):
12+
for x in sums[i]:
13+
if (x + num) * len(nums) == s * (i + 1):
14+
return True
15+
16+
sums[i + 1].add(x + num)
17+
18+
return False

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@
475475
[796][796l] |[Rotate String][796] |![rs]
476476
[799][799l] |[Champagne Tower][799] |![rs]
477477
[804][804l] |[Unique Morse Code Words][804] |![py]
478+
[805][805l] |[Split Array With Same Average][805] |![py]
478479
[806][806l] |[Number of Lines To Write String][806] |![py]
479480
[807][807l] |[Max Increase to Keep City Skyline][807] |![rs]
480481
[808][808l] |[Soup Servings][808] |![rs]
@@ -1871,6 +1872,7 @@
18711872
[796]:Problemset/0796-Rotate%20String/README.md#796-rotate-string
18721873
[799]:Problemset/0799-Champagne%20Tower/README.md#799-champagne-tower
18731874
[804]:Problemset/0804-Unique%20Morse%20Code%20Words/README.md#804-unique-morse-code-words
1875+
[805]:Problemset/0805-Split%20Array%20With%20Same%20Average/README.md#805-split-array-with-same-average
18741876
[806]:Problemset/0806-Number%20of%20Lines%20To%20Write%20String/README.md#806-number-of-lines-to-write-string
18751877
[807]:Problemset/0807-Max%20Increase%20to%20Keep%20City%20Skyline/README.md#807-max-increase-to-keep-city-skyline
18761878
[808]:Problemset/0808-Soup%20Servings/README.md#808-soup-servings
@@ -3266,6 +3268,7 @@
32663268
[796l]:https://leetcode.com/problems/rotate-string/
32673269
[799l]:https://leetcode.com/problems/champagne-tower/
32683270
[804l]:https://leetcode.com/problems/unique-morse-code-words/
3271+
[805l]:https://leetcode.com/problems/split-array-with-same-average/
32693272
[806l]:https://leetcode.com/problems/number-of-lines-to-write-string/
32703273
[807l]:https://leetcode.com/problems/max-increase-to-keep-city-skyline/
32713274
[808l]:https://leetcode.com/problems/soup-servings/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@
475475
[796][796l] |[旋转字符串][796] |![rs]
476476
[799][799l] |[香槟塔][799] |![rs]
477477
[804][804l] |[唯一摩尔斯密码词][804] |![py]
478+
[805][805l] |[数组的均值分割][805] |![py]
478479
[806][806l] |[写字符串需要的行数][806] |![py]
479480
[807][807l] |[保持城市天际线][807] |![rs]
480481
[808][808l] |[分汤][808] |![rs]
@@ -1871,6 +1872,7 @@
18711872
[796]:Problemset/0796-Rotate%20String/README_CN.md#796-旋转字符串
18721873
[799]:Problemset/0799-Champagne%20Tower/README_CN.md#799-香槟塔
18731874
[804]:Problemset/0804-Unique%20Morse%20Code%20Words/README_CN.md#804-唯一摩尔斯密码词
1875+
[805]:Problemset/0805-Split%20Array%20With%20Same%20Average/README_CN.md#805-数组的均值分割
18741876
[806]:Problemset/0806-Number%20of%20Lines%20To%20Write%20String/README_CN.md#806-写字符串需要的行数
18751877
[807]:Problemset/0807-Max%20Increase%20to%20Keep%20City%20Skyline/README_CN.md#807-保持城市天际线
18761878
[808]:Problemset/0808-Soup%20Servings/README_CN.md#808-分汤
@@ -3266,6 +3268,7 @@
32663268
[796l]:https://leetcode.cn/problems/rotate-string/
32673269
[799l]:https://leetcode.cn/problems/champagne-tower/
32683270
[804l]:https://leetcode.cn/problems/unique-morse-code-words/
3271+
[805l]:https://leetcode.cn/problems/split-array-with-same-average/
32693272
[806l]:https://leetcode.cn/problems/number-of-lines-to-write-string/
32703273
[807l]:https://leetcode.cn/problems/max-increase-to-keep-city-skyline/
32713274
[808l]:https://leetcode.cn/problems/soup-servings/

0 commit comments

Comments
 (0)