Skip to content

Commit 807cc5b

Browse files
Merge pull request #113 from romankurnovskii/problems-3577-3769-3759-3753-3747-3733-3732-3728-3727-3724-3723-3722
Add solutions and explanations for problems 3722, 3727, 3759
2 parents 4dfee7b + 8d732ac commit 807cc5b

File tree

10 files changed

+178
-380
lines changed

10 files changed

+178
-380
lines changed

books/All.md

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -8922,68 +8922,6 @@ class Solution:
89228922
return res
89238923
```
89248924

8925-
## 1431. Kids With the Greatest Number of Candies [Easy]
8926-
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/
8927-
8928-
### Explanation
8929-
8930-
## 1431. Kids With the Greatest Number of Candies [Easy]
8931-
8932-
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies
8933-
8934-
## Description
8935-
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.
8936-
8937-
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.
8938-
8939-
Note that multiple kids can have the greatest number of candies.
8940-
8941-
**Examples**
8942-
8943-
```text
8944-
Input: candies = [2,3,5,1,3], extraCandies = 3
8945-
Output: [true,true,true,false,true]
8946-
Explanation: If you give all extraCandies to:
8947-
- Kid 1, they will have 2 + 3 = 5 candies, which is the greatest among the kids.
8948-
- Kid 2, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
8949-
- Kid 3, they will have 5 + 3 = 8 candies, which is the greatest among the kids.
8950-
- Kid 4, they will have 1 + 3 = 4 candies, which is not the greatest among the kids.
8951-
- Kid 5, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
8952-
8953-
Input: candies = [4,2,1,1,2], extraCandies = 1
8954-
Output: [true,false,false,false,false]
8955-
8956-
Input: candies = [12,1,12], extraCandies = 10
8957-
Output: [true,false,true]
8958-
```
8959-
8960-
**Constraints**
8961-
8962-
```text
8963-
- n == candies.length
8964-
- 2 <= n <= 100
8965-
- 1 <= candies[i] <= 100
8966-
- 1 <= extraCandies <= 50
8967-
```
8968-
8969-
## Hint
8970-
For each kid, check if their candies plus `extraCandies` is at least as much as the current maximum.
8971-
8972-
## Explanation
8973-
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."
8974-
8975-
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.
8976-
8977-
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.
8978-
8979-
### Solution
8980-
8981-
```python
8982-
def kidsWithCandies(candies, extraCandies):
8983-
max_candies = max(candies) # Find the current maximum
8984-
return [(c + extraCandies) >= max_candies for c in candies]
8985-
```
8986-
89878925
## 1448. Count Good Nodes in Binary Tree [Medium]
89888926
https://leetcode.com/problems/count-good-nodes-in-binary-tree/
89898927

books/LeetCode_75.md

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -517,68 +517,6 @@ def uniqueOccurrences(arr):
517517
return len(set(count.values())) == len(count)
518518
```
519519

520-
## 1431. Kids With the Greatest Number of Candies [Easy]
521-
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/
522-
523-
### Explanation
524-
525-
## 1431. Kids With the Greatest Number of Candies [Easy]
526-
527-
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies
528-
529-
## Description
530-
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.
531-
532-
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.
533-
534-
Note that multiple kids can have the greatest number of candies.
535-
536-
**Examples**
537-
538-
```text
539-
Input: candies = [2,3,5,1,3], extraCandies = 3
540-
Output: [true,true,true,false,true]
541-
Explanation: If you give all extraCandies to:
542-
- Kid 1, they will have 2 + 3 = 5 candies, which is the greatest among the kids.
543-
- Kid 2, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
544-
- Kid 3, they will have 5 + 3 = 8 candies, which is the greatest among the kids.
545-
- Kid 4, they will have 1 + 3 = 4 candies, which is not the greatest among the kids.
546-
- Kid 5, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
547-
548-
Input: candies = [4,2,1,1,2], extraCandies = 1
549-
Output: [true,false,false,false,false]
550-
551-
Input: candies = [12,1,12], extraCandies = 10
552-
Output: [true,false,true]
553-
```
554-
555-
**Constraints**
556-
557-
```text
558-
- n == candies.length
559-
- 2 <= n <= 100
560-
- 1 <= candies[i] <= 100
561-
- 1 <= extraCandies <= 50
562-
```
563-
564-
## Hint
565-
For each kid, check if their candies plus `extraCandies` is at least as much as the current maximum.
566-
567-
## Explanation
568-
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."
569-
570-
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.
571-
572-
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.
573-
574-
### Solution
575-
576-
```python
577-
def kidsWithCandies(candies, extraCandies):
578-
max_candies = max(candies) # Find the current maximum
579-
return [(c + extraCandies) >= max_candies for c in candies]
580-
```
581-
582520
## 2215. Find the Difference of Two Arrays [Easy]
583521
https://leetcode.com/problems/find-the-difference-of-two-arrays/
584522

books/Visualization.md

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -86,68 +86,6 @@ def __init__(self, val=0, next=None):
8686
self.next = next
8787
```
8888

89-
## 1431. Kids With the Greatest Number of Candies [Easy]
90-
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/
91-
92-
### Explanation
93-
94-
## 1431. Kids With the Greatest Number of Candies [Easy]
95-
96-
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies
97-
98-
## Description
99-
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.
100-
101-
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.
102-
103-
Note that multiple kids can have the greatest number of candies.
104-
105-
**Examples**
106-
107-
```text
108-
Input: candies = [2,3,5,1,3], extraCandies = 3
109-
Output: [true,true,true,false,true]
110-
Explanation: If you give all extraCandies to:
111-
- Kid 1, they will have 2 + 3 = 5 candies, which is the greatest among the kids.
112-
- Kid 2, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
113-
- Kid 3, they will have 5 + 3 = 8 candies, which is the greatest among the kids.
114-
- Kid 4, they will have 1 + 3 = 4 candies, which is not the greatest among the kids.
115-
- Kid 5, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
116-
117-
Input: candies = [4,2,1,1,2], extraCandies = 1
118-
Output: [true,false,false,false,false]
119-
120-
Input: candies = [12,1,12], extraCandies = 10
121-
Output: [true,false,true]
122-
```
123-
124-
**Constraints**
125-
126-
```text
127-
- n == candies.length
128-
- 2 <= n <= 100
129-
- 1 <= candies[i] <= 100
130-
- 1 <= extraCandies <= 50
131-
```
132-
133-
## Hint
134-
For each kid, check if their candies plus `extraCandies` is at least as much as the current maximum.
135-
136-
## Explanation
137-
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."
138-
139-
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.
140-
141-
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.
142-
143-
### Solution
144-
145-
```python
146-
def kidsWithCandies(candies, extraCandies):
147-
max_candies = max(candies) # Find the current maximum
148-
return [(c + extraCandies) >= max_candies for c in candies]
149-
```
150-
15189
## 1798. Maximum Number of Consecutive Values You Can Make [Medium]
15290
https://leetcode.com/problems/maximum-number-of-consecutive-values-you-can-make/
15391

data/book-sets.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@
6161
1304, 1318, 1337, 1372, 1423, 1431, 1448, 1456, 1466, 1480, 1493, 1515, 1523, 1528, 1557, 1584, 1657, 1672, 1679, 1704, 1732, 1768, 1798, 1920,
6262
1925, 1926, 1929, 1957, 1963, 2011, 2095, 2119, 2130, 2211, 2215, 2300, 2336, 2352, 2390, 2419, 2462, 2542, 2627, 2703, 2723, 2769, 2807, 2862,
6363
2879, 2884, 2888, 2894, 2942, 3100, 3110, 3133, 3164, 3190, 3197, 3228, 3291, 3320, 3351, 3380, 3381, 3413, 3424, 3432, 3444, 3471, 3512, 3522,
64-
3602, 3603, 3606, 3607, 3608, 3622, 3623, 3625, 3663, 3668, 3678, 3683, 3688, 3692, 3697, 3701, 3707, 3712, 3718, 3722, 3723, 3724, 3726, 3727,
65-
3728, 3731, 3736, 3737, 3738, 3739, 3740, 3741, 3742, 3743, 3745, 3747, 3748, 3750, 3751, 3752, 3753, 3754, 3755, 3756, 3757, 3759, 3760, 3761,
66-
3762, 3764, 3765, 3766, 3767, 3768, 3769, 3770, 3771, 3772
64+
3583, 3602, 3603, 3606, 3607, 3608, 3622, 3623, 3625, 3663, 3668, 3678, 3683, 3688, 3692, 3697, 3701, 3707, 3712, 3718, 3722, 3723, 3724, 3726,
65+
3727, 3728, 3731, 3736, 3737, 3738, 3739, 3740, 3741, 3742, 3743, 3745, 3747, 3748, 3750, 3751, 3752, 3753, 3754, 3755, 3756, 3757, 3759, 3760,
66+
3761, 3764, 3765, 3766, 3767, 3768, 3770, 3771, 3772
6767
]
6868
},
6969
{"title": "Visualization", "description": "", "tags": [], "problems": [1, 2, 11, 1431, 1679, 1768, 1798, 2215, 3603, 3622, 3623]},

explanations/3722/en.md

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,67 @@
11
## Explanation
22

3-
### Strategy (The "Why")
3+
### Strategy
44

5-
**1.1 Constraints & Complexity:**
5+
**1.1 Constraints & Complexity**
66

7-
- **Constraints:** $1 \leq n \leq 1000$ where $n$ is the string length. String contains only lowercase English letters.
8-
- **Time Complexity:** $O(n^2)$ where $n$ is the string length. We try $2n$ operations, each involving string reversal which is $O(n)$.
9-
- **Space Complexity:** $O(n)$ for storing reversed strings.
10-
- **Edge Case:** If the string is already lexicographically smallest, return it unchanged.
7+
* **Input Size:** The string `s` has length `n` where `1 <= n <= 1000`. This is a small constraint that allows for a straightforward approach.
8+
* **Time Complexity:** O(n^2) - We try all possible values of `k` from 1 to `n` for both prefix and suffix reversals, and each reversal operation takes O(n) time.
9+
* **Space Complexity:** O(n) - We create new string candidates for comparison, but we only keep the best one at any time.
10+
* **Edge Case:** If no reversal produces a lexicographically smaller string, we return the original string.
1111

12-
**1.2 High-level approach:**
12+
**1.2 High-level approach**
1313

14-
The goal is to find the lexicographically smallest string after performing exactly one operation: either reversing the first $k$ characters or the last $k$ characters for some $k$. We try all possible operations and return the lexicographically smallest result.
14+
The goal is to find the lexicographically smallest string achievable by performing exactly one reversal operation (either on the first `k` characters or the last `k` characters).
1515

16-
**1.3 Brute force vs. optimized strategy:**
16+
Imagine you have a string written on cards. You can flip either the leftmost `k` cards or the rightmost `k` cards exactly once, and you want the resulting arrangement to be as small as possible when read left to right.
1717

18-
- **Brute Force:** Try all $2n$ possible operations (reverse first $k$ for $k=1$ to $n$, reverse last $k$ for $k=1$ to $n$), compare results. This is $O(n^2)$ time.
19-
- **Optimized Strategy:** Same as brute force - we need to check all possibilities since there's no obvious pattern to skip. This is $O(n^2)$ time.
20-
- **Why this approach:** The problem constraints allow $O(n^2)$ solution, and there's no obvious optimization to avoid checking all operations.
18+
**1.3 Brute force vs. optimized strategy**
2119

22-
**1.4 Decomposition:**
20+
* **Brute Force:** Try all possible values of `k` from 1 to `n` for prefix reversals, and all possible values of `k` from 1 to `n` for suffix reversals. For each candidate, compare it lexicographically with the current best result. This is exactly what we do, and it's efficient enough given the small constraint.
21+
* **Optimized Strategy:** The brute force approach is already optimal for this problem size. We systematically check all `2n` possible operations (n prefix reversals + n suffix reversals) and keep track of the lexicographically smallest result.
2322

24-
1. Initialize result as the original string.
25-
2. Try reversing first $k$ characters for $k = 1$ to $n$:
26-
- Reverse `s[:k]` and concatenate with `s[k:]`
27-
- Update result if this is lexicographically smaller.
28-
3. Try reversing last $k$ characters for $k = 1$ to $n$:
29-
- Keep `s[:n-k]` and reverse `s[n-k:]`
30-
- Update result if this is lexicographically smaller.
31-
4. Return the result.
23+
**1.4 Decomposition**
3224

33-
### Steps (The "How")
25+
1. Initialize the result with the original string as the baseline.
26+
2. Try all prefix reversals: For each `k` from 1 to `n`, reverse the first `k` characters and compare the result.
27+
3. Try all suffix reversals: For each `k` from 1 to `n`, reverse the last `k` characters and compare the result.
28+
4. Update the result whenever we find a lexicographically smaller candidate.
29+
5. Return the smallest string found.
3430

35-
**2.1 Initialization & Example Setup:**
31+
### Steps
3632

37-
Let's use the example: `s = "dcab"`
33+
**2.1 Initialization & Example Setup**
3834

39-
We initialize `res = "dcab"`.
35+
Let's use the example `s = "dcab"` to trace through the solution.
4036

41-
**2.2 Start Checking:**
37+
We initialize `res = "dcab"` as our current best result.
4238

43-
We try all possible reversal operations.
39+
**2.2 Start Checking/Processing**
4440

45-
**2.3 Trace Walkthrough:**
41+
We begin by trying all possible prefix reversals (reversing the first `k` characters) for `k` from 1 to `n`.
4642

47-
**Reverse first k characters:**
48-
- k=1: "d" + "cab" = "dcab" (not smaller)
49-
- k=2: "cd" + "ab" = "cdab" (not smaller)
50-
- k=3: "acd" + "b" = "acdb" (smaller! update res)
51-
- k=4: "bacd" (not smaller)
43+
**2.3 Trace Walkthrough**
5244

53-
**Reverse last k characters:**
54-
- k=1: "dca" + "b" = "dcab" (not smaller)
55-
- k=2: "dc" + "ba" = "dcba" (not smaller)
56-
- k=3: "d" + "bac" = "dbac" (not smaller)
57-
- k=4: "bacd" (not smaller)
45+
| k | Prefix to Reverse | Reversed Prefix | Remaining | Candidate | Is Smaller? | Update res? |
46+
|---|-------------------|-----------------|-----------|-----------|-------------|--------------|
47+
| 1 | "d" | "d" | "cab" | "dcab" | No | No |
48+
| 2 | "dc" | "cd" | "ab" | "cdab" | No | No |
49+
| 3 | "dca" | "acd" | "b" | "acdb" | Yes | Yes → "acdb" |
50+
| 4 | "dcab" | "bacd" | "" | "bacd" | No | No |
5851

59-
Best result: "acdb"
52+
Now we try suffix reversals (reversing the last `k` characters):
6053

61-
**2.4 Increment and Loop:**
54+
| k | Suffix to Reverse | Remaining | Reversed Suffix | Candidate | Is Smaller? | Update res? |
55+
|---|-------------------|-----------|-----------------|-----------|-------------|--------------|
56+
| 1 | "b" | "dca" | "b" | "dcab" | No | No |
57+
| 2 | "ab" | "dc" | "ba" | "dcba" | No | No |
58+
| 3 | "cab" | "d" | "bac" | "dbac" | No | No |
59+
| 4 | "dcab" | "" | "bacd" | "bacd" | No | No |
6260

63-
- For $k$ from 1 to $n$:
64-
- `reversed_str = s[:k][::-1] + s[k:]`
65-
- `if reversed_str < res: res = reversed_str`
66-
- For $k$ from 1 to $n$:
67-
- `reversed_str = s[:n-k] + s[n-k:][::-1]`
68-
- `if reversed_str < res: res = reversed_str`
61+
**2.4 Increment and Loop**
6962

70-
**2.5 Return Result:**
63+
After checking all prefix reversals, we move to checking all suffix reversals. We continue until we've examined all `2n` possible operations.
7164

72-
For `s = "dcab"`, the lexicographically smallest result is "acdb" (obtained by reversing the first 3 characters). We return "acdb".
65+
**2.5 Return Result**
7366

67+
The lexicographically smallest string found is `"acdb"`, which we return as the final result.

0 commit comments

Comments
 (0)