Skip to content

Commit 216886d

Browse files
committed
feat: add solutions to lc problems: No.2591~2598
* No.2591.Distribute Money to Maximum Children * No.2592.Maximize Greatness of an Array * No.2593.Find Score of an Array After Marking All Elements * No.2594.Minimum Time to Repair Cars * No.2595.Number of Even and Odd Bits * No.2596.Check Knight Tour Configuration * No.2597.The Number of Beautiful Subsets * No.2598.Smallest Missing Non-negative Integer After Operations
1 parent eb482dd commit 216886d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+2997
-12
lines changed

solution/2800-2899/2890.Design a Todo List/README.md solution/2500-2599/2590.Design a Todo List/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# [2890. Design a Todo List](https://leetcode.cn/problems/design-a-todo-list)
1+
# [2590. Design a Todo List](https://leetcode.cn/problems/design-a-todo-list)
22

3-
[English Version](/solution/2800-2899/2890.Design%20a%20Todo%20List/README_EN.md)
3+
[English Version](/solution/2500-2599/2590.Design%20a%20Todo%20List/README_EN.md)
44

55
## 题目描述
66

solution/2800-2899/2890.Design a Todo List/README_EN.md solution/2500-2599/2590.Design a Todo List/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# [2890. Design a Todo List](https://leetcode.com/problems/design-a-todo-list)
1+
# [2590. Design a Todo List](https://leetcode.com/problems/design-a-todo-list)
22

3-
[中文文档](/solution/2800-2899/2890.Design%20a%20Todo%20List/README.md)
3+
[中文文档](/solution/2500-2599/2590.Design%20a%20Todo%20List/README.md)
44

55
## Description
66

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# [2591. 将钱分给最多的儿童](https://leetcode.cn/problems/distribute-money-to-maximum-children)
2+
3+
[English Version](/solution/2500-2599/2591.Distribute%20Money%20to%20Maximum%20Children/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个整数&nbsp;<code>money</code>&nbsp;,表示你总共有的钱数(单位为美元)和另一个整数&nbsp;<code>children</code>&nbsp;,表示你要将钱分配给多少个儿童。</p>
10+
11+
<p>你需要按照如下规则分配:</p>
12+
13+
<ul>
14+
<li>所有的钱都必须被分配。</li>
15+
<li>每个儿童至少获得&nbsp;<code>1</code>&nbsp;美元。</li>
16+
<li>没有人获得 <code>4</code>&nbsp;美元。</li>
17+
</ul>
18+
19+
<p>请你按照上述规则分配金钱,并返回 <strong>最多</strong>&nbsp;有多少个儿童获得 <strong>恰好</strong><em>&nbsp;</em><code>8</code>&nbsp;美元。如果没有任何分配方案,返回&nbsp;<code>-1</code>&nbsp;。</p>
20+
21+
<p>&nbsp;</p>
22+
23+
<p><strong>示例 1:</strong></p>
24+
25+
<pre><b>输入:</b>money = 20, children = 3
26+
<b>输出:</b>1
27+
<b>解释:</b>
28+
最多获得 8 美元的儿童数为 1 。一种分配方案为:
29+
- 给第一个儿童分配 8 美元。
30+
- 给第二个儿童分配 9 美元。
31+
- 给第三个儿童分配 3 美元。
32+
没有分配方案能让获得 8 美元的儿童数超过 1 。
33+
</pre>
34+
35+
<p><strong>示例 2:</strong></p>
36+
37+
<pre><b>输入:</b>money = 16, children = 2
38+
<b>输出:</b>2
39+
<b>解释:</b>每个儿童都可以获得 8 美元。
40+
</pre>
41+
42+
<p>&nbsp;</p>
43+
44+
<p><strong>提示:</strong></p>
45+
46+
<ul>
47+
<li><code>1 &lt;= money &lt;= 200</code></li>
48+
<li><code>2 &lt;= children &lt;= 30</code></li>
49+
</ul>
50+
51+
## 解法
52+
53+
<!-- 这里可写通用的实现逻辑 -->
54+
55+
**方法一:分类讨论**
56+
57+
如果 $money \lt children$,那么一定存在儿童没有分到钱,返回 $-1$。
58+
59+
如果 $money \gt 8 \times children$,那么有 $children-1$ 个儿童获得了 $8$ 美元,剩下的一个儿童获得了 $money - 8 \times (children-1)$ 美元,返回 $children-1$。
60+
61+
如果 $money = 8 \times children - 4$,那么有 $children-2$ 个儿童获得了 $8$ 美元,剩下的两个儿童分摊剩下的 $12$ 美元(只要不是 $4$, $8$ 美元就行),返回 $children-2$。
62+
63+
如果,我们假设有 $x$ 个儿童获得了 $8$ 美元,那么剩下的钱为 $money- 8 \times x$,只要保证大于等于剩下的儿童数 $children-x$,就可以满足题意。因此,我们只需要求出 $x$ 的最大值,即为答案。
64+
65+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
66+
67+
<!-- tabs:start -->
68+
69+
### **Python3**
70+
71+
<!-- 这里可写当前语言的特殊实现逻辑 -->
72+
73+
```python
74+
class Solution:
75+
def distMoney(self, money: int, children: int) -> int:
76+
if money < children:
77+
return -1
78+
if money > 8 * children:
79+
return children - 1
80+
if money == 8 * children - 4:
81+
return children - 2
82+
# money-8x >= children-x, x <= (money-children)/7
83+
return (money - children) // 7
84+
```
85+
86+
### **Java**
87+
88+
<!-- 这里可写当前语言的特殊实现逻辑 -->
89+
90+
```java
91+
class Solution {
92+
public int distMoney(int money, int children) {
93+
if (money < children) {
94+
return -1;
95+
}
96+
if (money > 8 * children) {
97+
return children - 1;
98+
}
99+
if (money == 8 * children - 4) {
100+
return children - 2;
101+
}
102+
// money-8x >= children-x, x <= (money-children)/7
103+
return (money - children) / 7;
104+
}
105+
}
106+
```
107+
108+
### **C++**
109+
110+
```cpp
111+
class Solution {
112+
public:
113+
int distMoney(int money, int children) {
114+
if (money < children) {
115+
return -1;
116+
}
117+
if (money > 8 * children) {
118+
return children - 1;
119+
}
120+
if (money == 8 * children - 4) {
121+
return children - 2;
122+
}
123+
// money-8x >= children-x, x <= (money-children)/7
124+
return (money - children) / 7;
125+
}
126+
};
127+
```
128+
129+
### **Go**
130+
131+
```go
132+
func distMoney(money int, children int) int {
133+
if money < children {
134+
return -1
135+
}
136+
if money > 8*children {
137+
return children - 1
138+
}
139+
if money == 8*children-4 {
140+
return children - 2
141+
}
142+
// money-8x >= children-x, x <= (money-children)/7
143+
return (money - children) / 7
144+
}
145+
```
146+
147+
### **...**
148+
149+
```
150+
151+
```
152+
153+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# [2591. Distribute Money to Maximum Children](https://leetcode.com/problems/distribute-money-to-maximum-children)
2+
3+
[中文文档](/solution/2500-2599/2591.Distribute%20Money%20to%20Maximum%20Children/README.md)
4+
5+
## Description
6+
7+
<p>You are given an integer <code>money</code> denoting the amount of money (in dollars) that you have and another integer <code>children</code> denoting the number of children that you must distribute the money to.</p>
8+
9+
<p>You have to distribute the money according to the following rules:</p>
10+
11+
<ul>
12+
<li>All money must be distributed.</li>
13+
<li>Everyone must receive at least <code>1</code> dollar.</li>
14+
<li>Nobody receives <code>4</code> dollars.</li>
15+
</ul>
16+
17+
<p>Return <em>the <strong>maximum</strong> number of children who may receive <strong>exactly</strong> </em><code>8</code> <em>dollars if you distribute the money according to the aforementioned rules</em>. If there is no way to distribute the money, return <code>-1</code>.</p>
18+
19+
<p>&nbsp;</p>
20+
<p><strong class="example">Example 1:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> money = 20, children = 3
24+
<strong>Output:</strong> 1
25+
<strong>Explanation:</strong>
26+
The maximum number of children with 8 dollars will be 1. One of the ways to distribute the money is:
27+
- 8 dollars to the first child.
28+
- 9 dollars to the second child.
29+
- 3 dollars to the third child.
30+
It can be proven that no distribution exists such that number of children getting 8 dollars is greater than 1.
31+
</pre>
32+
33+
<p><strong class="example">Example 2:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong> money = 16, children = 2
37+
<strong>Output:</strong> 2
38+
<strong>Explanation:</strong> Each child can be given 8 dollars.
39+
</pre>
40+
41+
<p>&nbsp;</p>
42+
<p><strong>Constraints:</strong></p>
43+
44+
<ul>
45+
<li><code>1 &lt;= money &lt;= 200</code></li>
46+
<li><code>2 &lt;= children &lt;= 30</code></li>
47+
</ul>
48+
49+
## Solutions
50+
51+
<!-- tabs:start -->
52+
53+
### **Python3**
54+
55+
```python
56+
class Solution:
57+
def distMoney(self, money: int, children: int) -> int:
58+
if money < children:
59+
return -1
60+
if money > 8 * children:
61+
return children - 1
62+
if money == 8 * children - 4:
63+
return children - 2
64+
# money-8x >= children-x, x <= (money-children)/7
65+
return (money - children) // 7
66+
```
67+
68+
### **Java**
69+
70+
```java
71+
class Solution {
72+
public int distMoney(int money, int children) {
73+
if (money < children) {
74+
return -1;
75+
}
76+
if (money > 8 * children) {
77+
return children - 1;
78+
}
79+
if (money == 8 * children - 4) {
80+
return children - 2;
81+
}
82+
// money-8x >= children-x, x <= (money-children)/7
83+
return (money - children) / 7;
84+
}
85+
}
86+
```
87+
88+
### **C++**
89+
90+
```cpp
91+
class Solution {
92+
public:
93+
int distMoney(int money, int children) {
94+
if (money < children) {
95+
return -1;
96+
}
97+
if (money > 8 * children) {
98+
return children - 1;
99+
}
100+
if (money == 8 * children - 4) {
101+
return children - 2;
102+
}
103+
// money-8x >= children-x, x <= (money-children)/7
104+
return (money - children) / 7;
105+
}
106+
};
107+
```
108+
109+
### **Go**
110+
111+
```go
112+
func distMoney(money int, children int) int {
113+
if money < children {
114+
return -1
115+
}
116+
if money > 8*children {
117+
return children - 1
118+
}
119+
if money == 8*children-4 {
120+
return children - 2
121+
}
122+
// money-8x >= children-x, x <= (money-children)/7
123+
return (money - children) / 7
124+
}
125+
```
126+
127+
### **...**
128+
129+
```
130+
131+
```
132+
133+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int distMoney(int money, int children) {
4+
if (money < children) {
5+
return -1;
6+
}
7+
if (money > 8 * children) {
8+
return children - 1;
9+
}
10+
if (money == 8 * children - 4) {
11+
return children - 2;
12+
}
13+
// money-8x >= children-x, x <= (money-children)/7
14+
return (money - children) / 7;
15+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func distMoney(money int, children int) int {
2+
if money < children {
3+
return -1
4+
}
5+
if money > 8*children {
6+
return children - 1
7+
}
8+
if money == 8*children-4 {
9+
return children - 2
10+
}
11+
// money-8x >= children-x, x <= (money-children)/7
12+
return (money - children) / 7
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int distMoney(int money, int children) {
3+
if (money < children) {
4+
return -1;
5+
}
6+
if (money > 8 * children) {
7+
return children - 1;
8+
}
9+
if (money == 8 * children - 4) {
10+
return children - 2;
11+
}
12+
// money-8x >= children-x, x <= (money-children)/7
13+
return (money - children) / 7;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def distMoney(self, money: int, children: int) -> int:
3+
if money < children:
4+
return -1
5+
if money > 8 * children:
6+
return children - 1
7+
if money == 8 * children - 4:
8+
return children - 2
9+
# money-8x >= children-x, x <= (money-children)/7
10+
return (money - children) // 7

0 commit comments

Comments
 (0)