Skip to content

Commit 6a21afa

Browse files
committed
explanations
1 parent 3eaf78b commit 6a21afa

File tree

11 files changed

+550
-18
lines changed

11 files changed

+550
-18
lines changed

data/book-sets.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@
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,
6464
3577, 3583, 3602, 3603, 3606, 3607, 3608, 3622, 3623, 3625, 3663, 3668, 3678, 3683, 3688, 3692, 3697, 3701, 3707, 3712, 3718, 3722, 3723, 3724,
65-
3726, 3727, 3728, 3731, 3732, 3736, 3737, 3738, 3739, 3740, 3741, 3742, 3743, 3745, 3747, 3748, 3750, 3751, 3752, 3753, 3754, 3755, 3756, 3757,
66-
3759, 3760, 3761, 3764, 3765, 3766, 3767, 3768, 3770, 3771, 3772
65+
3726, 3727, 3728, 3731, 3732, 3733, 3736, 3737, 3738, 3739, 3740, 3741, 3742, 3743, 3745, 3747, 3748, 3750, 3751, 3752, 3753, 3754, 3755, 3756,
66+
3757, 3759, 3760, 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]},
@@ -181,8 +181,8 @@
181181
3626, 3627, 3628, 3629, 3630, 3633, 3634, 3635, 3636, 3637, 3638, 3639, 3640, 3642, 3643, 3644, 3645, 3646, 3648, 3649, 3650, 3651, 3652, 3653,
182182
3654, 3655, 3657, 3658, 3659, 3660, 3661, 3664, 3665, 3666, 3669, 3670, 3671, 3673, 3674, 3675, 3676, 3677, 3679, 3680, 3681, 3684, 3685, 3686,
183183
3689, 3690, 3691, 3693, 3694, 3695, 3698, 3699, 3700, 3702, 3703, 3704, 3705, 3706, 3708, 3709, 3710, 3711, 3713, 3714, 3715, 3716, 3717, 3719,
184-
3720, 3721, 3725, 3729, 3730, 3733, 3734, 3735, 3744, 3746, 3749, 3758, 3763, 3773, 3774, 3775, 3776, 3777, 3778, 3779, 3780, 3781, 3782, 3783,
185-
3784, 3785, 3786, 3787, 3788, 3789, 3790, 3791, 3792, 3793, 3794, 3795, 3796, 3797, 3798, 3799
184+
3720, 3721, 3725, 3729, 3730, 3734, 3735, 3744, 3746, 3749, 3758, 3763, 3773, 3774, 3775, 3776, 3777, 3778, 3779, 3780, 3781, 3782, 3783, 3784,
185+
3785, 3786, 3787, 3788, 3789, 3790, 3791, 3792, 3793, 3794, 3795, 3796, 3797, 3798, 3799
186186
],
187187
"premium": [
188188
27, 156, 157, 158, 159, 161, 163, 170, 186, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 259, 261, 265, 266, 267, 269,

explanations/3536/en.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
## Explanation
2+
3+
### Strategy (The "Why")
4+
5+
**1.1 Constraints & Complexity:**
6+
7+
* **Input Size:** The integer `n` can be up to 10^9, but we only process its digits (at most 10 digits).
8+
* **Time Complexity:** O(d^2) where d is the number of unique digits (at most 10), effectively O(1).
9+
* **Space Complexity:** O(d) for storing digit frequencies, effectively O(1).
10+
* **Edge Case:** When a digit appears only once, we cannot use it twice in the product.
11+
12+
**1.2 High-level approach:**
13+
14+
The goal is to find the maximum product of any two digits from the number. We need to consider all pairs of digits, but can only use the same digit twice if it appears more than once in the original number.
15+
16+
![Visualization showing digits extracted from a number and all possible pairs being considered]
17+
18+
**1.3 Brute force vs. optimized strategy:**
19+
20+
* **Brute Force:** Extract all digits, generate all pairs (including same digit twice), filter invalid pairs, find max. This is O(d^2) where d is number of digits.
21+
* **Optimized (Frequency-based):** Count digit frequencies first, then only consider pairs where same-digit pairs are only allowed if frequency > 1. This is also O(d^2) but more efficient in practice.
22+
* **Why it's better:** By checking frequencies upfront, we avoid generating invalid pairs and make the logic clearer.
23+
24+
**1.4 Decomposition:**
25+
26+
1. Extract all digits from the number and count their frequencies.
27+
2. Get unique digits from the frequency map.
28+
3. For each pair of unique digits, check if the pair is valid (same digit can only be used twice if frequency > 1).
29+
4. Calculate the product for valid pairs and track the maximum.
30+
5. Return the maximum product.
31+
32+
### Steps (The "How")
33+
34+
**2.1 Initialization & Example Setup:**
35+
36+
Let's use the example: `n = 124`
37+
38+
We initialize:
39+
* Extract digits: `digits = [4, 2, 1]` (extracted in reverse order)
40+
* Count frequencies: `digit_count = {1: 1, 2: 1, 4: 1}`
41+
* `unique_digits = [1, 2, 4]`
42+
* `res = 0`
43+
44+
**2.2 Start Checking:**
45+
46+
We iterate through all pairs of unique digits.
47+
48+
**2.3 Trace Walkthrough:**
49+
50+
| i | j | d1 | d2 | Same digit? | Frequency check | Product | res |
51+
| --- | --- | --- | --- | ----------- | --------------- | --------- | --- |
52+
| 0 | 0 | 1 | 1 | Yes | 1 > 1? No | Skip | 0 |
53+
| 0 | 1 | 1 | 2 | No | - | 1 * 2 = 2 | 2 |
54+
| 0 | 2 | 1 | 4 | No | - | 1 * 4 = 4 | 4 |
55+
| 1 | 1 | 2 | 2 | Yes | 1 > 1? No | Skip | 4 |
56+
| 1 | 2 | 2 | 4 | No | - | 2 * 4 = 8 | 8 |
57+
| 2 | 2 | 4 | 4 | Yes | 1 > 1? No | Skip | 8 |
58+
59+
**2.4 Increment and Loop:**
60+
61+
We continue checking all pairs (i, j) where i <= j to cover all combinations.
62+
63+
**2.5 Return Result:**
64+
65+
After checking all valid pairs, we return `res = 8`, which is the maximum product (2 * 4).

explanations/3577/en.md

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,29 @@
55
**1.1 Constraints & Complexity:**
66

77
* **Input Size:** The array `complexity` can have up to 10^5 elements.
8-
* **Time Complexity:** O(n) - We iterate through the array once to check conditions and compute factorial.
8+
* **Time Complexity:** O(n) - We iterate through the array once to check if complexity[0] is unique minimum, then compute factorial.
99
* **Space Complexity:** O(1) - We only use a constant amount of extra space.
10-
* **Edge Case:** If any computer (other than computer 0) has complexity less than or equal to computer 0, no valid permutations exist.
10+
* **Edge Case:** If complexity[0] is not the unique minimum (other elements have the same value), no valid permutations exist.
1111

1212
**1.2 High-level approach:**
1313

14-
The goal is to count valid permutations where computers can be unlocked in order. Computer 0 is already unlocked. Each computer i > 0 can only be unlocked using a previously unlocked computer j where j < i and complexity[j] < complexity[i]. Since computer 0 is the root, all other computers must be unlockable using computer 0, which means they must all have complexity greater than computer 0.
14+
The goal is to count valid permutations where computers can be unlocked in order. Computer 0 must be unlocked first and must have the unique minimum complexity. If this condition is satisfied, all remaining computers can be arranged in any order since they can all be unlocked using computer 0.
1515

16-
![Visualization showing computer 0 as root with arrows to other computers that must have higher complexity]
16+
![Visualization showing computer 0 as root with unique minimum complexity, with arrows to other computers that can be unlocked in any order]
1717

1818
**1.3 Brute force vs. optimized strategy:**
1919

2020
* **Brute Force:** Generate all permutations and check validity for each. This is O(n! * n) which is infeasible for large n.
21-
* **Optimized (Constraint Check + Factorial):** First verify that all computers i > 0 have complexity[i] > complexity[0]. If true, we can arrange the remaining n-1 computers in any order, giving us (n-1)! permutations. This is O(n) time.
21+
* **Optimized (Constraint Check + Factorial):** First verify that complexity[0] is the unique minimum. If true, we can arrange the remaining n-1 computers in any order, giving us (n-1)! permutations. This is O(n) time.
2222
* **Why it's better:** We avoid generating permutations by recognizing the mathematical structure - if the constraint is satisfied, all arrangements are valid.
2323

2424
**1.4 Decomposition:**
2525

26+
<<<<<<< HEAD
2627
1. Check if all computers i > 0 have complexity greater than computer 0.
2728
2. If any computer has complexity <= complexity[0], return 0 (no valid permutations).
28-
3. If the constraint is satisfied, compute (n-1)! modulo 10^9 + 7.
29+
2. If not unique, return 0 (no valid permutations).
30+
>>>>>>> 922cad0 (explanations)
2931
4. Return the result.
3032

3133
### Steps (The "How")
@@ -37,25 +39,27 @@ Let's use the example: `complexity = [1, 2, 3]`
3739
We initialize:
3840
* `MOD = 10^9 + 7` (for modulo arithmetic)
3941
* `n = 3`
40-
* `res = 1` (to compute factorial)
42+
* `min_complexity = 1` (complexity[0])
43+
* `min_count = 1` (only one element has value 1)
4144

4245
**2.2 Start Checking:**
4346

44-
We iterate through indices from 1 to n-1, checking if each computer can be unlocked.
47+
We check if `min_count > 1`. Since it's 1, we proceed to compute the factorial.
4548

4649
**2.3 Trace Walkthrough:**
4750

48-
| Index i | complexity[i] | complexity[0] | Check | Action | res |
49-
|---------|---------------|---------------|-------|--------|-----|
50-
| 1 | 2 | 1 | 2 > 1 ✓ | Continue, res = 1 * 1 = 1 | 1 |
51-
| 2 | 3 | 1 | 3 > 1 ✓ | Continue, res = 1 * 2 = 2 | 2 |
51+
| Step | i | Action | res |
52+
| ------- | --- | --------------- | --- |
53+
| Initial | - | res = 1 | 1 |
54+
| 1 | 1 | res = 1 * 1 = 1 | 1 |
55+
| 2 | 2 | res = 1 * 2 = 2 | 2 |
5256

5357
Since all checks pass, we return 2 (which is (3-1)! = 2!).
5458

5559
**2.4 Increment and Loop:**
5660

57-
For each valid index i, we multiply `res` by `i` (since we're computing (n-1)!, and i ranges from 1 to n-1).
61+
For each index i from 1 to n-1, we multiply `res` by `i` (since we're computing (n-1)!, and i ranges from 1 to n-1).
5862

5963
**2.5 Return Result:**
6064

61-
After checking all computers and computing the factorial, we return `res = 2`, representing the 2 valid permutations: [0, 1, 2] and [0, 2, 1].
65+
After checking that complexity[0] is unique and computing the factorial, we return `res = 2`, representing the 2 valid permutations: [0, 1, 2] and [0, 2, 1].

explanations/3582/en.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
## Explanation
2+
3+
### Strategy (The "Why")
4+
5+
**1.1 Constraints & Complexity:**
6+
7+
* **Input Size:** The string `caption` can have up to 150 characters.
8+
* **Time Complexity:** O(n) where n is the length of the caption. We process each character once.
9+
* **Space Complexity:** O(n) for building the result string.
10+
* **Edge Case:** If the caption is empty, return "#". If the result exceeds 100 characters, truncate it.
11+
12+
**1.2 High-level approach:**
13+
14+
The goal is to transform a caption into a hashtag by: (1) converting to camelCase with '#' prefix, (2) removing non-letter characters, (3) truncating to 100 characters. We process the string step by step according to these rules.
15+
16+
![Visualization showing caption being transformed: split into words, convert to camelCase, filter letters, truncate]
17+
18+
**1.3 Brute force vs. optimized strategy:**
19+
20+
* **Brute Force:** Process the string in multiple passes: split words, convert to camelCase, filter non-letters, truncate. This is O(n) which is optimal.
21+
* **Optimized:** Same as brute force - we need to process each character, so O(n) is the best we can do.
22+
* **Why it's better:** The approach is straightforward and processes the string efficiently in a single logical flow.
23+
24+
**1.4 Decomposition:**
25+
26+
1. Split the caption into words.
27+
2. Build camelCase string: first word lowercase, subsequent words with capitalized first letter.
28+
3. Filter out all non-letter characters except the initial '#'.
29+
4. Truncate to 100 characters if necessary.
30+
5. Return the result.
31+
32+
### Steps (The "How")
33+
34+
**2.1 Initialization & Example Setup:**
35+
36+
Let's use the example: `caption = "Leetcode daily streak achieved"`
37+
38+
We initialize:
39+
* Split words: `words = ["Leetcode", "daily", "streak", "achieved"]`
40+
* `res = "#"`
41+
42+
**2.2 Start Building:**
43+
44+
We process each word to build the camelCase string.
45+
46+
**2.3 Trace Walkthrough:**
47+
48+
| i | word | Action | res |
49+
| --- | ---------- | ------------------------------------ | ------------------------------ |
50+
| 0 | "Leetcode" | res += "leetcode" (lowercase) | "#leetcode" |
51+
| 1 | "daily" | res += "Daily" (capitalize first) | "#leetcodeDaily" |
52+
| 2 | "streak" | res += "Streak" (capitalize first) | "#leetcodeDailyStreak" |
53+
| 3 | "achieved" | res += "Achieved" (capitalize first) | "#leetcodeDailyStreakAchieved" |
54+
55+
After filtering non-letters (all are letters): `filtered = "#leetcodeDailyStreakAchieved"`
56+
Length check: 30 <= 100, no truncation needed.
57+
58+
**2.4 Increment and Loop:**
59+
60+
We continue processing each word, building the camelCase string, then filter and truncate.
61+
62+
**2.5 Return Result:**
63+
64+
After processing all words, filtering, and checking length, we return `"#leetcodeDailyStreakAchieved"`.

explanations/3591/en.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
## Explanation
2+
3+
### Strategy (The "Why")
4+
5+
**1.1 Constraints & Complexity:**
6+
7+
* **Input Size:** The array `nums` can have up to 100 elements.
8+
* **Time Complexity:** O(n + k * sqrt(m)) where n is array length, k is number of unique elements, and m is the maximum frequency. Since n <= 100 and frequencies are small, this is effectively O(n).
9+
* **Space Complexity:** O(k) where k is the number of unique elements, effectively O(n) in worst case.
10+
* **Edge Case:** If all elements have frequency 1 (which is not prime), return false.
11+
12+
**1.2 High-level approach:**
13+
14+
The goal is to check if any element in the array has a prime frequency. We count the frequency of each element, then check if any frequency is a prime number.
15+
16+
![Visualization showing array elements being counted and their frequencies being checked for primality]
17+
18+
**1.3 Brute force vs. optimized strategy:**
19+
20+
* **Brute Force:** For each unique element, count its frequency by scanning the entire array, then check if frequency is prime. This is O(n^2) if done naively.
21+
* **Optimized (Hash Map + Prime Check):** Use a hash map to count frequencies in one pass (O(n)), then check each frequency for primality (O(sqrt(m)) per frequency). This is O(n + k * sqrt(m)).
22+
* **Why it's better:** Single pass counting is more efficient, and we can optimize prime checking by only checking up to sqrt(n).
23+
24+
**1.4 Decomposition:**
25+
26+
1. Count the frequency of each element using a hash map.
27+
2. For each frequency value, check if it is a prime number.
28+
3. If any frequency is prime, return true.
29+
4. If no prime frequency is found, return false.
30+
31+
### Steps (The "How")
32+
33+
**2.1 Initialization & Example Setup:**
34+
35+
Let's use the example: `nums = [1, 2, 3, 4, 5, 4]`
36+
37+
We initialize:
38+
* Count frequencies: `freq = {1: 1, 2: 1, 3: 1, 4: 2, 5: 1}`
39+
* `res = false` (initially)
40+
41+
**2.2 Start Checking:**
42+
43+
We iterate through all frequency values and check if any is prime.
44+
45+
**2.3 Trace Walkthrough:**
46+
47+
| Element | Frequency | Is Prime? | Action | res |
48+
| ------- | --------- | ---------------- | ------------- | ----- |
49+
| 1 | 1 | No (1 < 2) | Continue | false |
50+
| 2 | 1 | No (1 < 2) | Continue | false |
51+
| 3 | 1 | No (1 < 2) | Continue | false |
52+
| 4 | 2 | Yes (2 is prime) | Return true | true |
53+
| 5 | 1 | No (1 < 2) | (not reached) | true |
54+
55+
**2.4 Increment and Loop:**
56+
57+
We check each frequency value. Once we find a prime frequency, we immediately return true.
58+
59+
**2.5 Return Result:**
60+
61+
Since element 4 has frequency 2 (which is prime), we return `true`.

explanations/3633/en.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
## Explanation
2+
3+
### Strategy (The "Why")
4+
5+
**1.1 Constraints & Complexity:**
6+
7+
* **Input Size:** Each array can have up to 100 elements.
8+
* **Time Complexity:** O(n * m) where n is number of land rides and m is number of water rides. We try all combinations of land and water rides in both orders.
9+
* **Space Complexity:** O(1) - We only use a constant amount of extra space.
10+
* **Edge Case:** If a ride finishes before the other ride opens, we must wait until the second ride opens.
11+
12+
**1.2 High-level approach:**
13+
14+
The goal is to find the earliest time to finish both rides (one land, one water) in either order. We try all combinations: each land ride with each water ride, considering both orders (land first or water first), and find the minimum finish time.
15+
16+
![Visualization showing timeline with land and water rides, trying different orders to find earliest finish time]
17+
18+
**1.3 Brute force vs. optimized strategy:**
19+
20+
* **Brute Force:** Try all combinations of land and water rides in both orders, calculate finish time for each, return minimum. This is O(n * m) which is optimal for this problem size.
21+
* **Optimized:** Same as brute force - the problem constraints are small enough that trying all combinations is efficient.
22+
* **Why it's better:** For small input sizes (n, m <= 100), the brute force approach is simple, clear, and efficient enough.
23+
24+
**1.4 Decomposition:**
25+
26+
1. Try all combinations of land rides and water rides.
27+
2. For each combination, try both orders: land then water, and water then land.
28+
3. Calculate the finish time for each order, accounting for waiting if the second ride hasn't opened yet.
29+
4. Track the minimum finish time across all combinations.
30+
5. Return the minimum finish time.
31+
32+
### Steps (The "How")
33+
34+
**2.1 Initialization & Example Setup:**
35+
36+
Let's use the example: `landStartTime = [2, 8]`, `landDuration = [4, 1]`, `waterStartTime = [6]`, `waterDuration = [3]`
37+
38+
We initialize:
39+
* `res = infinity` (to track minimum)
40+
41+
**2.2 Start Checking:**
42+
43+
We iterate through all land rides and water rides, trying both orders.
44+
45+
**2.3 Trace Walkthrough:**
46+
47+
| land_idx | water_idx | Order | Land Finish | Water Start | Water Finish | res |
48+
| -------- | --------- | ---------- | ----------- | ----------- | ------------ | --- |
49+
| 0 | 0 | Land→Water | 2+4=6 | max(6,6)=6 | 6+3=9 | 9 |
50+
| 0 | 0 | Water→Land | 6+3=9 | max(2,9)=9 | 9+4=13 | 9 |
51+
| 1 | 0 | Land→Water | 8+1=9 | max(6,9)=9 | 9+3=12 | 9 |
52+
| 1 | 0 | Water→Land | 6+3=9 | max(8,9)=9 | 9+1=10 | 9 |
53+
54+
**2.4 Increment and Loop:**
55+
56+
We continue trying all combinations and update `res` with the minimum finish time found.
57+
58+
**2.5 Return Result:**
59+
60+
After checking all combinations, we return `res = 9`, which is the earliest finish time (land ride 0 → water ride 0).

0 commit comments

Comments
 (0)