|
1 | | -## 1431. Kids With the Greatest Number of Candies [Easy] |
| 1 | +## Explanation |
2 | 2 |
|
3 | | -https://leetcode.com/problems/kids-with-the-greatest-number-of-candies |
| 3 | +### Strategy (The "Why") |
4 | 4 |
|
5 | | -## Description |
6 | | -There are `n` kids with candies. You are given an integer array `candies`, where each `candies[i]` represents the number of candies the `i`th kid has, and an integer `extraCandies`, denoting the number of extra candies that you have. |
| 5 | +Given an array `candies` representing the number of candies each kid has, and `extraCandies` extra candies, we need to determine for each kid whether giving them all the extra candies would make them have the greatest number of candies. |
7 | 6 |
|
8 | | -Return a boolean array `result` of length `n`, where `result[i]` is `true` if, after giving the `i`th kid all the `extraCandies`, they will have the greatest number of candies among all the kids, or false otherwise. |
| 7 | +**1.1 Constraints & Complexity:** |
9 | 8 |
|
10 | | -Note that multiple kids can have the greatest number of candies. |
| 9 | +- **Input Size:** The number of kids n is between 2 and 100. |
| 10 | +- **Value Range:** Each kid has between 1 and 100 candies, and extraCandies is between 1 and 50. |
| 11 | +- **Time Complexity:** O(n) - We iterate through the candies array twice: once to find the maximum, and once to build the result. |
| 12 | +- **Space Complexity:** O(n) - We create a result list of length n. |
| 13 | +- **Edge Case:** If all kids have the same number of candies, all will be true (since adding extraCandies to any will make them have the most). |
11 | 14 |
|
12 | | -**Examples** |
| 15 | +**1.2 High-level approach:** |
13 | 16 |
|
14 | | -```text |
15 | | -Input: candies = [2,3,5,1,3], extraCandies = 3 |
16 | | -Output: [true,true,true,false,true] |
17 | | -Explanation: If you give all extraCandies to: |
18 | | -- Kid 1, they will have 2 + 3 = 5 candies, which is the greatest among the kids. |
19 | | -- Kid 2, they will have 3 + 3 = 6 candies, which is the greatest among the kids. |
20 | | -- Kid 3, they will have 5 + 3 = 8 candies, which is the greatest among the kids. |
21 | | -- Kid 4, they will have 1 + 3 = 4 candies, which is not the greatest among the kids. |
22 | | -- Kid 5, they will have 3 + 3 = 6 candies, which is the greatest among the kids. |
| 17 | +The goal is to check, for each kid, if their current candies plus the extra candies would be greater than or equal to the maximum number of candies any kid currently has. |
23 | 18 |
|
24 | | -Input: candies = [4,2,1,1,2], extraCandies = 1 |
25 | | -Output: [true,false,false,false,false] |
| 19 | + |
26 | 20 |
|
27 | | -Input: candies = [12,1,12], extraCandies = 10 |
28 | | -Output: [true,false,true] |
29 | | -``` |
| 21 | +We first find the maximum number of candies, then for each kid, we check if `candies[i] + extraCandies >= max_candies`. |
30 | 22 |
|
31 | | -**Constraints** |
| 23 | +**1.3 Brute force vs. optimized strategy:** |
32 | 24 |
|
33 | | -```text |
34 | | -- n == candies.length |
35 | | -- 2 <= n <= 100 |
36 | | -- 1 <= candies[i] <= 100 |
37 | | -- 1 <= extraCandies <= 50 |
38 | | -``` |
| 25 | +- **Brute Force:** For each kid, add extraCandies to their count, then check if this new count is greater than all other kids' counts. This would be O(n²) because for each kid we compare with all others. |
| 26 | +- **Optimized Strategy (Two-Pass):** First pass: find the maximum number of candies. Second pass: for each kid, check if their candies plus extraCandies is >= the maximum. This is O(n). |
| 27 | +- **Why it's better:** By finding the maximum first, we only need one comparison per kid in the second pass, reducing time complexity from O(n²) to O(n). |
39 | 28 |
|
40 | | -## Hint |
41 | | -For each kid, check if their candies plus `extraCandies` is at least as much as the current maximum. |
| 29 | +**1.4 Decomposition:** |
42 | 30 |
|
43 | | -## Explanation |
44 | | -First, you want to know the highest number of candies any kid currently has. This is important because you need a reference point to see if giving extra candies to a kid will make them "the greatest." |
| 31 | +1. Find the maximum number of candies in the array. |
| 32 | +2. Initialize an empty result list. |
| 33 | +3. For each kid, check if their candies plus extraCandies is greater than or equal to the maximum. |
| 34 | +4. Append True or False to the result list accordingly. |
| 35 | +5. Return the result list. |
| 36 | + |
| 37 | +### Steps (The "How") |
| 38 | + |
| 39 | +**2.1 Initialization & Example Setup:** |
| 40 | + |
| 41 | +Let's use the example: `candies = [2, 3, 5, 1, 3]`, `extraCandies = 3` |
| 42 | + |
| 43 | +We initialize: |
| 44 | +- `max_candies = 0` (will find maximum) |
| 45 | +- `res = []` (result list) |
| 46 | + |
| 47 | +**2.2 Start Checking:** |
| 48 | + |
| 49 | +First, we find the maximum: `max_candies = max(candies) = 5` |
| 50 | + |
| 51 | +**2.3 Trace Walkthrough:** |
| 52 | + |
| 53 | +| Kid Index | Current Candies | Current + Extra | >= Max (5)? | res | |
| 54 | +|-----------|----------------|-----------------|-------------|-----| |
| 55 | +| 0 | 2 | 2 + 3 = 5 | Yes (5 >= 5) | [True] | |
| 56 | +| 1 | 3 | 3 + 3 = 6 | Yes (6 >= 5) | [True, True] | |
| 57 | +| 2 | 5 | 5 + 3 = 8 | Yes (8 >= 5) | [True, True, True] | |
| 58 | +| 3 | 1 | 1 + 3 = 4 | No (4 < 5) | [True, True, True, False] | |
| 59 | +| 4 | 3 | 3 + 3 = 6 | Yes (6 >= 5) | [True, True, True, False, True] | |
| 60 | + |
| 61 | +**2.4 Increment and Loop:** |
| 62 | + |
| 63 | +- For each kid, we calculate `candies[i] + extraCandies`. |
| 64 | +- We compare this sum with `max_candies`. |
| 65 | +- If the sum is >= max_candies, we append True; otherwise, we append False. |
| 66 | + |
| 67 | +**2.5 Return Result:** |
45 | 68 |
|
46 | | -For each kid, you add the `extraCandies` to their current amount. You do this because you want to see if, after the bonus, they can reach or beat the current maximum. If they do, you mark them as `True` in our answer list; otherwise, False. |
| 69 | +We return `[True, True, True, False, True]`, indicating that kids at indices 0, 1, 2, and 4 would have the greatest number of candies after receiving the extra candies. |
47 | 70 |
|
48 | | -You only need to find the maximum once, and then just compare each kid's total to it. Don't need to recalculate the maximum for every kid. |
| 71 | +> **Note:** We compare with the original maximum, not the maximum after giving extra candies. This is correct because if a kid's candies plus extraCandies is >= the original maximum, they will have at least as many as the kid who originally had the most. |
0 commit comments