Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 0 additions & 62 deletions books/All.md
Original file line number Diff line number Diff line change
Expand Up @@ -8922,68 +8922,6 @@ class Solution:
return res
```

## 1431. Kids With the Greatest Number of Candies [Easy]
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/

### Explanation

## 1431. Kids With the Greatest Number of Candies [Easy]

https://leetcode.com/problems/kids-with-the-greatest-number-of-candies

## Description
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.

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.

Note that multiple kids can have the greatest number of candies.

**Examples**

```text
Input: candies = [2,3,5,1,3], extraCandies = 3
Output: [true,true,true,false,true]
Explanation: If you give all extraCandies to:
- Kid 1, they will have 2 + 3 = 5 candies, which is the greatest among the kids.
- Kid 2, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
- Kid 3, they will have 5 + 3 = 8 candies, which is the greatest among the kids.
- Kid 4, they will have 1 + 3 = 4 candies, which is not the greatest among the kids.
- Kid 5, they will have 3 + 3 = 6 candies, which is the greatest among the kids.

Input: candies = [4,2,1,1,2], extraCandies = 1
Output: [true,false,false,false,false]

Input: candies = [12,1,12], extraCandies = 10
Output: [true,false,true]
```

**Constraints**

```text
- n == candies.length
- 2 <= n <= 100
- 1 <= candies[i] <= 100
- 1 <= extraCandies <= 50
```

## Hint
For each kid, check if their candies plus `extraCandies` is at least as much as the current maximum.

## Explanation
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."

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.

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.

### Solution

```python
def kidsWithCandies(candies, extraCandies):
max_candies = max(candies) # Find the current maximum
return [(c + extraCandies) >= max_candies for c in candies]
```

## 1448. Count Good Nodes in Binary Tree [Medium]
https://leetcode.com/problems/count-good-nodes-in-binary-tree/

Expand Down
62 changes: 0 additions & 62 deletions books/LeetCode_75.md
Original file line number Diff line number Diff line change
Expand Up @@ -517,68 +517,6 @@ def uniqueOccurrences(arr):
return len(set(count.values())) == len(count)
```

## 1431. Kids With the Greatest Number of Candies [Easy]
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/

### Explanation

## 1431. Kids With the Greatest Number of Candies [Easy]

https://leetcode.com/problems/kids-with-the-greatest-number-of-candies

## Description
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.

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.

Note that multiple kids can have the greatest number of candies.

**Examples**

```text
Input: candies = [2,3,5,1,3], extraCandies = 3
Output: [true,true,true,false,true]
Explanation: If you give all extraCandies to:
- Kid 1, they will have 2 + 3 = 5 candies, which is the greatest among the kids.
- Kid 2, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
- Kid 3, they will have 5 + 3 = 8 candies, which is the greatest among the kids.
- Kid 4, they will have 1 + 3 = 4 candies, which is not the greatest among the kids.
- Kid 5, they will have 3 + 3 = 6 candies, which is the greatest among the kids.

Input: candies = [4,2,1,1,2], extraCandies = 1
Output: [true,false,false,false,false]

Input: candies = [12,1,12], extraCandies = 10
Output: [true,false,true]
```

**Constraints**

```text
- n == candies.length
- 2 <= n <= 100
- 1 <= candies[i] <= 100
- 1 <= extraCandies <= 50
```

## Hint
For each kid, check if their candies plus `extraCandies` is at least as much as the current maximum.

## Explanation
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."

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.

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.

### Solution

```python
def kidsWithCandies(candies, extraCandies):
max_candies = max(candies) # Find the current maximum
return [(c + extraCandies) >= max_candies for c in candies]
```

## 2215. Find the Difference of Two Arrays [Easy]
https://leetcode.com/problems/find-the-difference-of-two-arrays/

Expand Down
62 changes: 0 additions & 62 deletions books/Visualization.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,68 +86,6 @@ def __init__(self, val=0, next=None):
self.next = next
```

## 1431. Kids With the Greatest Number of Candies [Easy]
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/

### Explanation

## 1431. Kids With the Greatest Number of Candies [Easy]

https://leetcode.com/problems/kids-with-the-greatest-number-of-candies

## Description
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.

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.

Note that multiple kids can have the greatest number of candies.

**Examples**

```text
Input: candies = [2,3,5,1,3], extraCandies = 3
Output: [true,true,true,false,true]
Explanation: If you give all extraCandies to:
- Kid 1, they will have 2 + 3 = 5 candies, which is the greatest among the kids.
- Kid 2, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
- Kid 3, they will have 5 + 3 = 8 candies, which is the greatest among the kids.
- Kid 4, they will have 1 + 3 = 4 candies, which is not the greatest among the kids.
- Kid 5, they will have 3 + 3 = 6 candies, which is the greatest among the kids.

Input: candies = [4,2,1,1,2], extraCandies = 1
Output: [true,false,false,false,false]

Input: candies = [12,1,12], extraCandies = 10
Output: [true,false,true]
```

**Constraints**

```text
- n == candies.length
- 2 <= n <= 100
- 1 <= candies[i] <= 100
- 1 <= extraCandies <= 50
```

## Hint
For each kid, check if their candies plus `extraCandies` is at least as much as the current maximum.

## Explanation
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."

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.

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.

### Solution

```python
def kidsWithCandies(candies, extraCandies):
max_candies = max(candies) # Find the current maximum
return [(c + extraCandies) >= max_candies for c in candies]
```

## 1798. Maximum Number of Consecutive Values You Can Make [Medium]
https://leetcode.com/problems/maximum-number-of-consecutive-values-you-can-make/

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

### Strategy (The "Why")
### Strategy

**1.1 Constraints & Complexity:**
**1.1 Constraints & Complexity**

- **Constraints:** $1 \leq n \leq 1000$ where $n$ is the string length. String contains only lowercase English letters.
- **Time Complexity:** $O(n^2)$ where $n$ is the string length. We try $2n$ operations, each involving string reversal which is $O(n)$.
- **Space Complexity:** $O(n)$ for storing reversed strings.
- **Edge Case:** If the string is already lexicographically smallest, return it unchanged.
* **Input Size:** The string `s` has length `n` where `1 <= n <= 1000`. This is a small constraint that allows for a straightforward approach.
* **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.
* **Space Complexity:** O(n) - We create new string candidates for comparison, but we only keep the best one at any time.
* **Edge Case:** If no reversal produces a lexicographically smaller string, we return the original string.

**1.2 High-level approach:**
**1.2 High-level approach**

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.
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).

**1.3 Brute force vs. optimized strategy:**
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.

- **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.
- **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.
- **Why this approach:** The problem constraints allow $O(n^2)$ solution, and there's no obvious optimization to avoid checking all operations.
**1.3 Brute force vs. optimized strategy**

**1.4 Decomposition:**
* **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.
* **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.

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

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

**2.1 Initialization & Example Setup:**
### Steps

Let's use the example: `s = "dcab"`
**2.1 Initialization & Example Setup**

We initialize `res = "dcab"`.
Let's use the example `s = "dcab"` to trace through the solution.

**2.2 Start Checking:**
We initialize `res = "dcab"` as our current best result.

We try all possible reversal operations.
**2.2 Start Checking/Processing**

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

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

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

Best result: "acdb"
Now we try suffix reversals (reversing the last `k` characters):

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

- For $k$ from 1 to $n$:
- `reversed_str = s[:k][::-1] + s[k:]`
- `if reversed_str < res: res = reversed_str`
- For $k$ from 1 to $n$:
- `reversed_str = s[:n-k] + s[n-k:][::-1]`
- `if reversed_str < res: res = reversed_str`
**2.4 Increment and Loop**

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

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

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