Skip to content

Commit 0e9d040

Browse files
committed
feat: add solutions to lc problem: No.1431
No.1431.Kids With the Greatest Number of Candies
1 parent 1129964 commit 0e9d040

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed

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

+41
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,47 @@ func max(a, b int) int {
123123
}
124124
```
125125

126+
### **TypeScript**
127+
128+
```ts
129+
function kidsWithCandies(candies: number[], extraCandies: number): boolean[] {
130+
const max = candies.reduce((r, v) => Math.max(r, v));
131+
return candies.map(v => v + extraCandies >= max);
132+
}
133+
```
134+
135+
### **Rust**
136+
137+
```rust
138+
impl Solution {
139+
pub fn kids_with_candies(candies: Vec<i32>, extra_candies: i32) -> Vec<bool> {
140+
let max = *candies.iter().max().unwrap();
141+
candies.iter().map(|v| v + extra_candies >= max).collect()
142+
}
143+
}
144+
```
145+
146+
### **C**
147+
148+
```c
149+
#define max(a, b) (((a) > (b)) ? (a) : (b))
150+
/**
151+
* Note: The returned array must be malloced, assume caller calls free().
152+
*/
153+
bool *kidsWithCandies(int *candies, int candiesSize, int extraCandies, int *returnSize) {
154+
int mx = 0;
155+
for (int i = 0; i < candiesSize; i++) {
156+
mx = max(mx, candies[i]);
157+
}
158+
bool *ans = malloc(candiesSize * sizeof(bool));
159+
for (int i = 0; i < candiesSize; i++) {
160+
ans[i] = candies[i] + extraCandies >= mx;
161+
}
162+
*returnSize = candiesSize;
163+
return ans;
164+
}
165+
```
166+
126167
### **...**
127168
128169
```

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

+41
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,47 @@ func max(a, b int) int {
120120
}
121121
```
122122

123+
### **TypeScript**
124+
125+
```ts
126+
function kidsWithCandies(candies: number[], extraCandies: number): boolean[] {
127+
const max = candies.reduce((r, v) => Math.max(r, v));
128+
return candies.map(v => v + extraCandies >= max);
129+
}
130+
```
131+
132+
### **Rust**
133+
134+
```rust
135+
impl Solution {
136+
pub fn kids_with_candies(candies: Vec<i32>, extra_candies: i32) -> Vec<bool> {
137+
let max = *candies.iter().max().unwrap();
138+
candies.iter().map(|v| v + extra_candies >= max).collect()
139+
}
140+
}
141+
```
142+
143+
### **C**
144+
145+
```c
146+
#define max(a, b) (((a) > (b)) ? (a) : (b))
147+
/**
148+
* Note: The returned array must be malloced, assume caller calls free().
149+
*/
150+
bool *kidsWithCandies(int *candies, int candiesSize, int extraCandies, int *returnSize) {
151+
int mx = 0;
152+
for (int i = 0; i < candiesSize; i++) {
153+
mx = max(mx, candies[i]);
154+
}
155+
bool *ans = malloc(candiesSize * sizeof(bool));
156+
for (int i = 0; i < candiesSize; i++) {
157+
ans[i] = candies[i] + extraCandies >= mx;
158+
}
159+
*returnSize = candiesSize;
160+
return ans;
161+
}
162+
```
163+
123164
### **...**
124165
125166
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#define max(a, b) (((a) > (b)) ? (a) : (b))
2+
/**
3+
* Note: The returned array must be malloced, assume caller calls free().
4+
*/
5+
bool *kidsWithCandies(int *candies, int candiesSize, int extraCandies, int *returnSize) {
6+
int mx = 0;
7+
for (int i = 0; i < candiesSize; i++) {
8+
mx = max(mx, candies[i]);
9+
}
10+
bool *ans = malloc(candiesSize * sizeof(bool));
11+
for (int i = 0; i < candiesSize; i++) {
12+
ans[i] = candies[i] + extraCandies >= mx;
13+
}
14+
*returnSize = candiesSize;
15+
return ans;
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
impl Solution {
2+
pub fn kids_with_candies(candies: Vec<i32>, extra_candies: i32) -> Vec<bool> {
3+
let max = *candies.iter().max().unwrap();
4+
candies.iter().map(|v| v + extra_candies >= max).collect()
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function kidsWithCandies(candies: number[], extraCandies: number): boolean[] {
2+
const max = candies.reduce((r, v) => Math.max(r, v));
3+
return candies.map(v => v + extraCandies >= max);
4+
}

0 commit comments

Comments
 (0)