Skip to content

Commit 2d2b2bd

Browse files
committed
+ problem 1655
1 parent db55c4f commit 2d2b2bd

File tree

5 files changed

+155
-0
lines changed

5 files changed

+155
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# 1655. Distribute Repeating Integers
2+
You are given an array of `n` integers, `nums`, where there are at most `50` unique values in the array. You are also given an array of `m` customer order quantities, `quantity`, where `quantity[i]` is the amount of integers the <code>i<sup>th</sup></code> customer ordered. Determine if it is possible to distribute `nums` such that:
3+
* The <code>i<sup>th</sup></code> customer gets **exactly** `quantity[i]` integers,
4+
* The integers the <code>i<sup>th</sup></code> customer gets are **all equal**, and
5+
* Every customer is satisfied.
6+
7+
Return `true` *if it is possible to distribute* `nums` *according to the above conditions*.
8+
9+
#### Example 1:
10+
<pre>
11+
<strong>Input:</strong> nums = [1,2,3,4], quantity = [2]
12+
<strong>Output:</strong> false
13+
<strong>Explanation:</strong> The 0th customer cannot be given two different integers.
14+
</pre>
15+
16+
#### Example 2:
17+
<pre>
18+
<strong>Input:</strong> nums = [1,2,3,3], quantity = [2]
19+
<strong>Output:</strong> true
20+
<strong>Explanation:</strong> The 0th customer is given [3,3]. The integers [1,2] are not used.
21+
</pre>
22+
23+
#### Example 3:
24+
<pre>
25+
<strong>Input:</strong> nums = [1,1,2,2], quantity = [2,2]
26+
<strong>Output:</strong> true
27+
<strong>Explanation:</strong> The 0th customer is given [1,1], and the 1st customer is given [2,2].
28+
</pre>
29+
30+
#### Constraints:
31+
* `n == nums.length`
32+
* <code>1 <= n <= 10<sup>5</sup></code>
33+
* `1 <= nums[i] <= 1000`
34+
* `m == quantity.length`
35+
* `1 <= m <= 10`
36+
* <code>1 <= quantity[i] <= 10<sup>5</sup></code>
37+
* There are at most `50` unique values in `nums`.
38+
39+
## Solutions (Python)
40+
41+
### 1. Solution
42+
```Python
43+
class Solution:
44+
def canDistribute(self, nums: List[int], quantity: List[int]) -> bool:
45+
def dfs(i: int) -> bool:
46+
if i == len(quantity):
47+
return True
48+
49+
for j in range(len(count)):
50+
if count[j] >= quantity[i] and (j == 0 or count[j] != count[j - 1]):
51+
count[j] -= quantity[i]
52+
if dfs(i + 1):
53+
return True
54+
count[j] += quantity[i]
55+
56+
return False
57+
58+
count = sorted(collections.Counter(nums).values())[-len(quantity):]
59+
60+
if count[-1] < max(quantity) or sum(count) < sum(quantity):
61+
return False
62+
63+
return dfs(0)
64+
```
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# 1655. 分配重复整数
2+
给你一个长度为 `n` 的整数数组 `nums` ,这个数组中至多有 `50` 个不同的值。同时你有 `m` 个顾客的订单 `quantity` ,其中,整数 `quantity[i]` 是第 `i` 位顾客订单的数目。请你判断是否能将 `nums` 中的整数分配给这些顾客,且满足:
3+
*`i` 位顾客 **恰好**`quantity[i]` 个整数。
4+
*`i` 位顾客拿到的整数都是 **相同的**
5+
* 每位顾客都满足上述两个要求。
6+
7+
如果你可以分配 `nums` 中的整数满足上面的要求,那么请返回 `true` ,否则返回 `false`
8+
9+
#### 示例 1:
10+
<pre>
11+
<strong>输入:</strong> nums = [1,2,3,4], quantity = [2]
12+
<strong>输出:</strong> false
13+
<strong>解释:</strong> 第 0 位顾客没办法得到两个相同的整数。
14+
</pre>
15+
16+
#### 示例 2:
17+
<pre>
18+
<strong>输入:</strong> nums = [1,2,3,3], quantity = [2]
19+
<strong>输出:</strong> true
20+
<strong>解释:</strong> 第 0 位顾客得到 [3,3] 。整数 [1,2] 都没有被使用。
21+
</pre>
22+
23+
#### 示例 3:
24+
<pre>
25+
<strong>输入:</strong> nums = [1,1,2,2], quantity = [2,2]
26+
<strong>输出:</strong> true
27+
<strong>解释:</strong> 第 0 位顾客得到 [1,1] ,第 1 位顾客得到 [2,2] 。
28+
</pre>
29+
30+
#### 提示:
31+
* `n == nums.length`
32+
* <code>1 <= n <= 10<sup>5</sup></code>
33+
* `1 <= nums[i] <= 1000`
34+
* `m == quantity.length`
35+
* `1 <= m <= 10`
36+
* <code>1 <= quantity[i] <= 10<sup>5</sup></code>
37+
* `nums` 中至多有 `50` 个不同的数字。
38+
39+
## 题解 (Python)
40+
41+
### 1. 题解
42+
```Python
43+
class Solution:
44+
def canDistribute(self, nums: List[int], quantity: List[int]) -> bool:
45+
def dfs(i: int) -> bool:
46+
if i == len(quantity):
47+
return True
48+
49+
for j in range(len(count)):
50+
if count[j] >= quantity[i] and (j == 0 or count[j] != count[j - 1]):
51+
count[j] -= quantity[i]
52+
if dfs(i + 1):
53+
return True
54+
count[j] += quantity[i]
55+
56+
return False
57+
58+
count = sorted(collections.Counter(nums).values())[-len(quantity):]
59+
60+
if count[-1] < max(quantity) or sum(count) < sum(quantity):
61+
return False
62+
63+
return dfs(0)
64+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def canDistribute(self, nums: List[int], quantity: List[int]) -> bool:
3+
def dfs(i: int) -> bool:
4+
if i == len(quantity):
5+
return True
6+
7+
for j in range(len(count)):
8+
if count[j] >= quantity[i] and (j == 0 or count[j] != count[j - 1]):
9+
count[j] -= quantity[i]
10+
if dfs(i + 1):
11+
return True
12+
count[j] += quantity[i]
13+
14+
return False
15+
16+
count = sorted(collections.Counter(nums).values())[-len(quantity):]
17+
18+
if count[-1] < max(quantity) or sum(count) < sum(quantity):
19+
return False
20+
21+
return dfs(0)

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,7 @@
11271127
[1652][1652l]|[Defuse the Bomb][1652] |![rb]&nbsp;&nbsp;![rs]
11281128
[1653][1653l]|[Minimum Deletions to Make String Balanced][1653] |![rs]
11291129
[1654][1654l]|[Minimum Jumps to Reach Home][1654] |![rs]
1130+
[1655][1655l]|[Distribute Repeating Integers][1655] |![py]
11301131
[1656][1656l]|[Design an Ordered Stream][1656] |![py]
11311132
[1657][1657l]|[Determine if Two Strings Are Close][1657] |![py]
11321133
[1658][1658l]|[Minimum Operations to Reduce X to Zero][1658] |![rb]&nbsp;&nbsp;![rs]
@@ -2867,6 +2868,7 @@
28672868
[1652]:Problemset/1652-Defuse%20the%20Bomb/README.md#1652-defuse-the-bomb
28682869
[1653]:Problemset/1653-Minimum%20Deletions%20to%20Make%20String%20Balanced/README.md#1653-minimum-deletions-to-make-string-balanced
28692870
[1654]:Problemset/1654-Minimum%20Jumps%20to%20Reach%20Home/README.md#1654-minimum-jumps-to-reach-home
2871+
[1655]:Problemset/1655-Distribute%20Repeating%20Integers/README.md#1655-distribute-repeating-integers
28702872
[1656]:Problemset/1656-Design%20an%20Ordered%20Stream/README.md#1656-design-an-ordered-stream
28712873
[1657]:Problemset/1657-Determine%20if%20Two%20Strings%20Are%20Close/README.md#1657-determine-if-two-strings-are-close
28722874
[1658]:Problemset/1658-Minimum%20Operations%20to%20Reduce%20X%20to%20Zero/README.md#1658-minimum-operations-to-reduce-x-to-zero
@@ -4601,6 +4603,7 @@
46014603
[1652l]:https://leetcode.com/problems/defuse-the-bomb/
46024604
[1653l]:https://leetcode.com/problems/minimum-deletions-to-make-string-balanced/
46034605
[1654l]:https://leetcode.com/problems/minimum-jumps-to-reach-home/
4606+
[1655l]:https://leetcode.com/problems/distribute-repeating-integers/
46044607
[1656l]:https://leetcode.com/problems/design-an-ordered-stream/
46054608
[1657l]:https://leetcode.com/problems/determine-if-two-strings-are-close/
46064609
[1658l]:https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,7 @@
11271127
[1652][1652l]|[拆炸弹][1652] |![rb]&nbsp;&nbsp;![rs]
11281128
[1653][1653l]|[使字符串平衡的最少删除次数][1653] |![rs]
11291129
[1654][1654l]|[到家的最少跳跃次数][1654] |![rs]
1130+
[1655][1655l]|[分配重复整数][1655] |![py]
11301131
[1656][1656l]|[设计有序流][1656] |![py]
11311132
[1657][1657l]|[确定两个字符串是否接近][1657] |![py]
11321133
[1658][1658l]|[将 x 减到 0 的最小操作数][1658] |![rb]&nbsp;&nbsp;![rs]
@@ -2867,6 +2868,7 @@
28672868
[1652]:Problemset/1652-Defuse%20the%20Bomb/README_CN.md#1652-拆炸弹
28682869
[1653]:Problemset/1653-Minimum%20Deletions%20to%20Make%20String%20Balanced/README_CN.md#1653-使字符串平衡的最少删除次数
28692870
[1654]:Problemset/1654-Minimum%20Jumps%20to%20Reach%20Home/README_CN.md#1654-到家的最少跳跃次数
2871+
[1655]:Problemset/1655-Distribute%20Repeating%20Integers/README_CN.md#1655-分配重复整数
28702872
[1656]:Problemset/1656-Design%20an%20Ordered%20Stream/README_CN.md#1656-设计有序流
28712873
[1657]:Problemset/1657-Determine%20if%20Two%20Strings%20Are%20Close/README_CN.md#1657-确定两个字符串是否接近
28722874
[1658]:Problemset/1658-Minimum%20Operations%20to%20Reduce%20X%20to%20Zero/README_CN.md#1658-将-x-减到-0-的最小操作数
@@ -4601,6 +4603,7 @@
46014603
[1652l]:https://leetcode.cn/problems/defuse-the-bomb/
46024604
[1653l]:https://leetcode.cn/problems/minimum-deletions-to-make-string-balanced/
46034605
[1654l]:https://leetcode.cn/problems/minimum-jumps-to-reach-home/
4606+
[1655l]:https://leetcode.cn/problems/distribute-repeating-integers/
46044607
[1656l]:https://leetcode.cn/problems/design-an-ordered-stream/
46054608
[1657l]:https://leetcode.cn/problems/determine-if-two-strings-are-close/
46064609
[1658l]:https://leetcode.cn/problems/minimum-operations-to-reduce-x-to-zero/

0 commit comments

Comments
 (0)