Skip to content

Commit 431d872

Browse files
committed
feat: add solutions to lc problem: No.1431. Kids With the Greatest
Number of Candies
1 parent 21ade09 commit 431d872

File tree

7 files changed

+155
-22
lines changed

7 files changed

+155
-22
lines changed

solution/0000-0099/0064.Minimum Path Sum/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
<!-- 这里可写通用的实现逻辑 -->
4444

45-
动态规划。假设 `dp[i][j]` 表示到达网格 `(i,j)` 的最小数字和,先初始化 dp 第一列和第一行的所有值,然后利用递推公式:`dp[i][j] = min(dp[i -1][j], dp[i][j - 1]) + grid[i][j]` 求得 dp。
45+
动态规划。假设 `dp[i][j]` 表示到达网格 `(i,j)` 的最小数字和,先初始化 dp 第一列和第一行的所有值,然后利用递推公式:`dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]` 求得 dp。
4646

4747
最后返回 `dp[m - 1][n - 1]` 即可。
4848

solution/1400-1499/1431.Kids With the Greatest Number of Candies/README.md

+55-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
<li><code>1 &lt;= extraCandies &lt;= 50</code></li>
4848
</ul>
4949

50-
5150
## 解法
5251

5352
<!-- 这里可写通用的实现逻辑 -->
@@ -59,15 +58,69 @@
5958
<!-- 这里可写当前语言的特殊实现逻辑 -->
6059

6160
```python
62-
61+
class Solution:
62+
def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]:
63+
mx = max(candies)
64+
return [candy + extraCandies >= mx for candy in candies]
6365
```
6466

6567
### **Java**
6668

6769
<!-- 这里可写当前语言的特殊实现逻辑 -->
6870

6971
```java
72+
class Solution {
73+
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
74+
int mx = 0;
75+
for (int candy : candies) {
76+
mx = Math.max(mx, candy);
77+
}
78+
List<Boolean> res = new ArrayList<>();
79+
for (int candy : candies) {
80+
res.add(candy + extraCandies >= mx);
81+
}
82+
return res;
83+
}
84+
}
85+
```
86+
87+
### **C++**
88+
89+
```cpp
90+
class Solution {
91+
public:
92+
vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {
93+
int mx = *max_element(candies.begin(), candies.end());
94+
vector<bool> res;
95+
for (int candy : candies) {
96+
res.push_back(candy + extraCandies >= mx);
97+
}
98+
return res;
99+
}
100+
};
101+
```
70102
103+
### **Go**
104+
105+
```go
106+
func kidsWithCandies(candies []int, extraCandies int) []bool {
107+
mx := 0
108+
for _, candy := range candies {
109+
mx = max(mx, candy)
110+
}
111+
var res []bool
112+
for _, candy := range candies {
113+
res = append(res, candy+extraCandies >= mx)
114+
}
115+
return res
116+
}
117+
118+
func max(a, b int) int {
119+
if a > b {
120+
return a
121+
}
122+
return b
123+
}
71124
```
72125

73126
### **...**

solution/1400-1499/1431.Kids With the Greatest Number of Candies/README_EN.md

+55-19
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,12 @@
66

77
<p>Given the array <code>candies</code> and the integer <code>extraCandies</code>, where <code>candies[i]</code> represents the number of candies that the <strong><em>ith</em></strong> kid has.</p>
88

9-
10-
119
<p>For each kid check if there is a way to distribute <code>extraCandies</code> among the kids such that he or she can have the <strong>greatest</strong> number of candies among them.&nbsp;Notice that multiple kids can have the <strong>greatest</strong> number of candies.</p>
1210

13-
14-
1511
<p>&nbsp;</p>
1612

1713
<p><strong>Example 1:</strong></p>
1814

19-
20-
2115
<pre>
2216

2317
<strong>Input:</strong> candies = [2,3,5,1,3], extraCandies = 3
@@ -38,12 +32,8 @@ Kid 5 has 3 candies and if he or she receives at least 2 extra candies will have
3832

3933
</pre>
4034

41-
42-
4335
<p><strong>Example 2:</strong></p>
4436

45-
46-
4737
<pre>
4838

4939
<strong>Input:</strong> candies = [4,2,1,1,2], extraCandies = 1
@@ -54,12 +44,8 @@ Kid 5 has 3 candies and if he or she receives at least 2 extra candies will have
5444

5545
</pre>
5646

57-
58-
5947
<p><strong>Example 3:</strong></p>
6048

61-
62-
6349
<pre>
6450

6551
<strong>Input:</strong> candies = [12,1,12], extraCandies = 10
@@ -68,14 +54,10 @@ Kid 5 has 3 candies and if he or she receives at least 2 extra candies will have
6854

6955
</pre>
7056

71-
72-
7357
<p>&nbsp;</p>
7458

7559
<p><strong>Constraints:</strong></p>
7660

77-
78-
7961
<ul>
8062
<li><code>2 &lt;= candies.length &lt;= 100</code></li>
8163
<li><code>1 &lt;= candies[i] &lt;= 100</code></li>
@@ -89,13 +71,67 @@ Kid 5 has 3 candies and if he or she receives at least 2 extra candies will have
8971
### **Python3**
9072

9173
```python
92-
74+
class Solution:
75+
def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]:
76+
mx = max(candies)
77+
return [candy + extraCandies >= mx for candy in candies]
9378
```
9479

9580
### **Java**
9681

9782
```java
83+
class Solution {
84+
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
85+
int mx = 0;
86+
for (int candy : candies) {
87+
mx = Math.max(mx, candy);
88+
}
89+
List<Boolean> res = new ArrayList<>();
90+
for (int candy : candies) {
91+
res.add(candy + extraCandies >= mx);
92+
}
93+
return res;
94+
}
95+
}
96+
```
97+
98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {
104+
int mx = *max_element(candies.begin(), candies.end());
105+
vector<bool> res;
106+
for (int candy : candies) {
107+
res.push_back(candy + extraCandies >= mx);
108+
}
109+
return res;
110+
}
111+
};
112+
```
98113
114+
### **Go**
115+
116+
```go
117+
func kidsWithCandies(candies []int, extraCandies int) []bool {
118+
mx := 0
119+
for _, candy := range candies {
120+
mx = max(mx, candy)
121+
}
122+
var res []bool
123+
for _, candy := range candies {
124+
res = append(res, candy+extraCandies >= mx)
125+
}
126+
return res
127+
}
128+
129+
func max(a, b int) int {
130+
if a > b {
131+
return a
132+
}
133+
return b
134+
}
99135
```
100136

101137
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {
4+
int mx = *max_element(candies.begin(), candies.end());
5+
vector<bool> res;
6+
for (int candy : candies) {
7+
res.push_back(candy + extraCandies >= mx);
8+
}
9+
return res;
10+
}
11+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func kidsWithCandies(candies []int, extraCandies int) []bool {
2+
mx := 0
3+
for _, candy := range candies {
4+
mx = max(mx, candy)
5+
}
6+
var res []bool
7+
for _, candy := range candies {
8+
res = append(res, candy+extraCandies >= mx)
9+
}
10+
return res
11+
}
12+
13+
func max(a, b int) int {
14+
if a > b {
15+
return a
16+
}
17+
return b
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {
4+
int mx = *max_element(candies.begin(), candies.end());
5+
vector<bool> res;
6+
for (int candy : candies) {
7+
res.push_back(candy + extraCandies >= mx);
8+
}
9+
return res;
10+
}
11+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]:
3+
mx = max(candies)
4+
return [candy + extraCandies >= mx for candy in candies]

0 commit comments

Comments
 (0)