Skip to content

Commit b08341e

Browse files
Added 1431-kids-with-the-greatest-number-of-candies
1 parent 855f262 commit b08341e

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
id: kids-with-the-greatest-number-of-candies
3+
title: Kids With the Greatest Number of Candies
4+
level: easy
5+
sidebar_label: Kids With the Greatest Number of Candies
6+
tags:
7+
- Array
8+
- Greedy
9+
- Java
10+
description: "This document provides solutions for the Kids With the Greatest Number of Candies problem."
11+
---
12+
13+
## Problem Statement
14+
15+
There are `n` kids with candies. You are given an integer array `candies`, where each `candies[i]` represents the number of candies the ith kid has, and an integer `extraCandies`, denoting the number of extra candies that you have.
16+
17+
Return a boolean array `result` of length `n`, where `result[i]` is `true` if, after giving the ith kid all the `extraCandies`, they will have the greatest number of candies among all the kids, or `false` otherwise.
18+
19+
**Example 1:**
20+
21+
Input: `candies = [2,3,5,1,3]`, `extraCandies = 3`
22+
23+
Output: `[true,true,true,false,true]`
24+
25+
Explanation: If you give all `extraCandies` to:
26+
- Kid 1, they will have `2 + 3 = 5` candies, which is the greatest among the kids.
27+
- Kid 2, they will have `3 + 3 = 6` candies, which is the greatest among the kids.
28+
- Kid 3, they will have `5 + 3 = 8` candies, which is the greatest among the kids.
29+
- Kid 4, they will have `1 + 3 = 4` candies, which is not the greatest among the kids.
30+
- Kid 5, they will have `3 + 3 = 6` candies, which is the greatest among the kids.
31+
32+
**Example 2:**
33+
34+
Input: `candies = [4,2,1,1,2]`, `extraCandies = 1`
35+
36+
Output: `[true,false,false,false,false]`
37+
38+
Explanation: There is only `1` extra candy.
39+
Kid 1 will always have the greatest number of candies, even if a different kid is given the extra candy.
40+
41+
**Example 3:**
42+
43+
Input: `candies = [12,1,12]`, `extraCandies = 10`
44+
45+
Output: `[true,false,true]`
46+
47+
**Constraints:**
48+
49+
- `n == candies.length`
50+
- `2 <= n <= 100`
51+
- `1 <= candies[i] <= 100`
52+
- `1 <= extraCandies <= 50`
53+
54+
## Solutions
55+
56+
### Approach
57+
58+
To determine if each kid can have the greatest number of candies after receiving `extraCandies`, follow these steps:
59+
60+
1. **Find the Maximum Candies:**
61+
- Traverse through the `candies` array to find the maximum number of candies any kid currently has.
62+
63+
2. **Check Each Kid's Condition:**
64+
- Iterate through the `candies` array again.
65+
- For each kid, check if adding `extraCandies` to their current candies makes their total candies equal to or greater than the maximum number found.
66+
- Store `true` if the condition is met, otherwise store `false`.
67+
68+
### Java
69+
70+
```java
71+
72+
class Solution {
73+
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
74+
int maxCandies = 0;
75+
for (int candy : candies) {
76+
maxCandies = Math.max(maxCandies, candy);
77+
}
78+
79+
List<Boolean> result = new ArrayList<>();
80+
for (int candy : candies) {
81+
result.add(candy + extraCandies >= maxCandies);
82+
}
83+
84+
return result;
85+
}
86+
87+
}
88+
```
89+
90+
### Python
91+
```Python
92+
class Solution:
93+
def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]:
94+
maxCandies = max(candies)
95+
result = []
96+
97+
for candy in candies:
98+
result.append(candy + extraCandies >= maxCandies)
99+
100+
return result
101+
```

0 commit comments

Comments
 (0)