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
20 changes: 10 additions & 10 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,
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, 3732, 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
3577, 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, 3732, 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 Expand Up @@ -176,13 +176,13 @@
3475, 3477, 3478, 3479, 3480, 3482, 3483, 3484, 3485, 3486, 3487, 3488, 3489, 3490, 3492, 3493, 3494, 3495, 3497, 3498, 3499, 3500, 3501, 3502,
3503, 3504, 3505, 3507, 3508, 3509, 3510, 3513, 3514, 3515, 3516, 3517, 3518, 3519, 3521, 3523, 3524, 3525, 3527, 3528, 3529, 3530, 3531, 3532,
3533, 3534, 3536, 3537, 3538, 3539, 3541, 3542, 3543, 3544, 3545, 3546, 3547, 3548, 3550, 3551, 3552, 3553, 3554, 3556, 3557, 3558, 3559, 3560,
3561, 3562, 3563, 3564, 3566, 3567, 3568, 3569, 3570, 3572, 3573, 3574, 3575, 3576, 3577, 3578, 3579, 3580, 3582, 3584, 3585, 3586, 3587, 3588,
3589, 3590, 3591, 3592, 3593, 3594, 3597, 3598, 3599, 3600, 3601, 3604, 3605, 3609, 3611, 3612, 3613, 3614, 3615, 3617, 3618, 3619, 3620, 3621,
3624, 3626, 3627, 3628, 3629, 3630, 3633, 3634, 3635, 3636, 3637, 3638, 3639, 3640, 3642, 3643, 3644, 3645, 3646, 3648, 3649, 3650, 3651, 3652,
3653, 3654, 3655, 3657, 3658, 3659, 3660, 3661, 3664, 3665, 3666, 3669, 3670, 3671, 3673, 3674, 3675, 3676, 3677, 3679, 3680, 3681, 3684, 3685,
3686, 3689, 3690, 3691, 3693, 3694, 3695, 3698, 3699, 3700, 3702, 3703, 3704, 3705, 3706, 3708, 3709, 3710, 3711, 3713, 3714, 3715, 3716, 3717,
3719, 3720, 3721, 3725, 3729, 3730, 3733, 3734, 3735, 3744, 3746, 3749, 3758, 3763, 3773, 3774, 3775, 3776, 3777, 3778, 3779, 3780, 3781, 3782,
3783, 3784, 3785, 3786, 3787, 3788, 3789, 3790, 3791, 3792, 3793, 3794, 3795, 3796, 3797, 3798, 3799
3561, 3562, 3563, 3564, 3566, 3567, 3568, 3569, 3570, 3572, 3573, 3574, 3575, 3576, 3578, 3579, 3580, 3582, 3584, 3585, 3586, 3587, 3588, 3589,
3590, 3591, 3592, 3593, 3594, 3597, 3598, 3599, 3600, 3601, 3604, 3605, 3609, 3611, 3612, 3613, 3614, 3615, 3617, 3618, 3619, 3620, 3621, 3624,
3626, 3627, 3628, 3629, 3630, 3633, 3634, 3635, 3636, 3637, 3638, 3639, 3640, 3642, 3643, 3644, 3645, 3646, 3648, 3649, 3650, 3651, 3652, 3653,
3654, 3655, 3657, 3658, 3659, 3660, 3661, 3664, 3665, 3666, 3669, 3670, 3671, 3673, 3674, 3675, 3676, 3677, 3679, 3680, 3681, 3684, 3685, 3686,
3689, 3690, 3691, 3693, 3694, 3695, 3698, 3699, 3700, 3702, 3703, 3704, 3705, 3706, 3708, 3709, 3710, 3711, 3713, 3714, 3715, 3716, 3717, 3719,
3720, 3721, 3725, 3729, 3730, 3733, 3734, 3735, 3744, 3746, 3749, 3758, 3763, 3773, 3774, 3775, 3776, 3777, 3778, 3779, 3780, 3781, 3782, 3783,
3784, 3785, 3786, 3787, 3788, 3789, 3790, 3791, 3792, 3793, 3794, 3795, 3796, 3797, 3798, 3799
],
"premium": [
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,
Expand Down
61 changes: 61 additions & 0 deletions explanations/3577/en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
## Explanation

### Strategy (The "Why")

**1.1 Constraints & Complexity:**

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

**1.2 High-level approach:**

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.

![Visualization showing computer 0 as root with arrows to other computers that must have higher complexity]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Missing visualization image.

The markdown references an image that doesn't exist in the repository:

![Visualization showing computer 0 as root with arrows to other computers that must have higher complexity]

Either provide the image file or remove this line if the visualization isn't available yet.

Do you want me to generate sample code or suggestions for creating this visualization diagram?

🤖 Prompt for AI Agents
In explanations/3577/en.md around line 16 the markdown references a non-existent
image: the line "![Visualization showing computer 0 as root with arrows to other
computers that must have higher complexity]". Fix by either (A) adding the
missing image file to the repo at a relative path and updating the markdown to
point to that path (ensure the filename and extension match exactly and commit
the image), or (B) remove the image reference line if the visualization is not
available yet; if you add an image, keep a clear descriptive filename (e.g.,
explanations/3577/visualization.png) and commit it with the markdown change so
the link resolves.


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

* **Brute Force:** Generate all permutations and check validity for each. This is O(n! * n) which is infeasible for large n.
* **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.
* **Why it's better:** We avoid generating permutations by recognizing the mathematical structure - if the constraint is satisfied, all arrangements are valid.

**1.4 Decomposition:**

1. Check if all computers i > 0 have complexity greater than computer 0.
2. If any computer has complexity <= complexity[0], return 0 (no valid permutations).
3. If the constraint is satisfied, compute (n-1)! modulo 10^9 + 7.
4. Return the result.

### Steps (The "How")

**2.1 Initialization & Example Setup:**

Let's use the example: `complexity = [1, 2, 3]`

We initialize:
* `MOD = 10^9 + 7` (for modulo arithmetic)
* `n = 3`
* `res = 1` (to compute factorial)

**2.2 Start Checking:**

We iterate through indices from 1 to n-1, checking if each computer can be unlocked.

**2.3 Trace Walkthrough:**

| Index i | complexity[i] | complexity[0] | Check | Action | res |
|---------|---------------|---------------|-------|--------|-----|
| 1 | 2 | 1 | 2 > 1 ✓ | Continue, res = 1 * 1 = 1 | 1 |
| 2 | 3 | 1 | 3 > 1 ✓ | Continue, res = 1 * 2 = 2 | 2 |

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

**2.4 Increment and Loop:**

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

**2.5 Return Result:**

After checking all computers and computing the factorial, we return `res = 2`, representing the 2 valid permutations: [0, 1, 2] and [0, 2, 1].
21 changes: 5 additions & 16 deletions solutions/3577/01.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
from typing import List


class Solution:
def countPermutations(self, complexity: List[int]) -> int:
def countPermutations(self, complexity: list[int]) -> int:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Check Python version requirements in the repository

# Look for Python version specifications
echo "=== Checking pyproject.toml ==="
fd -t f '^pyproject.toml$' -x cat {}

echo -e "\n=== Checking setup.py ==="
fd -t f '^setup.py$' -x cat {}

echo -e "\n=== Checking .python-version ==="
fd -t f '^\.python-version$' -x cat {}

echo -e "\n=== Checking other Python files for type hint style ==="
rg -t py "from typing import List" -C 2

Repository: romankurnovskii/leetcode-apps

Length of output: 11914


Use List[int] from typing module for consistency with codebase.

The type hint list[int] (PEP 585) requires Python 3.9+. The entire codebase consistently uses from typing import List with List[int] syntax. Update this method to follow the established pattern:

from typing import List

def countPermutations(self, complexity: List[int]) -> int:
🤖 Prompt for AI Agents
In solutions/3577/01.py around line 2, the method signature uses the PEP 585
form `list[int]`; change it to use the codebase-consistent typing import by
adding `from typing import List` (if not already present) and update the
signature to `def countPermutations(self, complexity: List[int]) -> int:` so the
parameter type uses `List[int]`.

MOD = 10**9 + 7

n = len(complexity)

# Check if complexity[0] is the unique minimum
min_complexity = complexity[0]
min_count = sum(1 for c in complexity if c == min_complexity)

if min_count > 1:
# If there are other elements with the same complexity as index 0, no valid permutations
return 0

# Index 0 must be first. The remaining n-1 indices can be arranged in any order
# So the answer is (n-1)!
# All computers i > 0 must have complexity[i] > complexity[0]
# If any computer has complexity <= complexity[0], it can never be unlocked
res = 1
for i in range(1, n):
if complexity[i] <= complexity[0]:
return 0
res = (res * i) % MOD

return res