Skip to content

Commit 3eaf78b

Browse files
Merge pull request #114 from romankurnovskii/problems-3577-3769-3753-3743-3739-3734-3733-3729-2728-3668-3637-3633-3606-3591-3582-3536
Add solution and explanation for problem 3577: Count the Number of Computer Unlocking Permutations
2 parents 670faf3 + 93a2745 commit 3eaf78b

File tree

3 files changed

+76
-26
lines changed

3 files changed

+76
-26
lines changed

data/book-sets.json

Lines changed: 10 additions & 10 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-
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, 3732, 3736, 3737, 3738, 3739, 3740, 3741, 3742, 3743, 3745, 3747, 3748, 3750, 3751, 3752, 3753, 3754, 3755, 3756, 3757, 3759,
66-
3760, 3761, 3764, 3765, 3766, 3767, 3768, 3770, 3771, 3772
64+
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
6767
]
6868
},
6969
{"title": "Visualization", "description": "", "tags": [], "problems": [1, 2, 11, 1431, 1679, 1768, 1798, 2215, 3603, 3622, 3623]},
@@ -176,13 +176,13 @@
176176
3475, 3477, 3478, 3479, 3480, 3482, 3483, 3484, 3485, 3486, 3487, 3488, 3489, 3490, 3492, 3493, 3494, 3495, 3497, 3498, 3499, 3500, 3501, 3502,
177177
3503, 3504, 3505, 3507, 3508, 3509, 3510, 3513, 3514, 3515, 3516, 3517, 3518, 3519, 3521, 3523, 3524, 3525, 3527, 3528, 3529, 3530, 3531, 3532,
178178
3533, 3534, 3536, 3537, 3538, 3539, 3541, 3542, 3543, 3544, 3545, 3546, 3547, 3548, 3550, 3551, 3552, 3553, 3554, 3556, 3557, 3558, 3559, 3560,
179-
3561, 3562, 3563, 3564, 3566, 3567, 3568, 3569, 3570, 3572, 3573, 3574, 3575, 3576, 3577, 3578, 3579, 3580, 3582, 3584, 3585, 3586, 3587, 3588,
180-
3589, 3590, 3591, 3592, 3593, 3594, 3597, 3598, 3599, 3600, 3601, 3604, 3605, 3609, 3611, 3612, 3613, 3614, 3615, 3617, 3618, 3619, 3620, 3621,
181-
3624, 3626, 3627, 3628, 3629, 3630, 3633, 3634, 3635, 3636, 3637, 3638, 3639, 3640, 3642, 3643, 3644, 3645, 3646, 3648, 3649, 3650, 3651, 3652,
182-
3653, 3654, 3655, 3657, 3658, 3659, 3660, 3661, 3664, 3665, 3666, 3669, 3670, 3671, 3673, 3674, 3675, 3676, 3677, 3679, 3680, 3681, 3684, 3685,
183-
3686, 3689, 3690, 3691, 3693, 3694, 3695, 3698, 3699, 3700, 3702, 3703, 3704, 3705, 3706, 3708, 3709, 3710, 3711, 3713, 3714, 3715, 3716, 3717,
184-
3719, 3720, 3721, 3725, 3729, 3730, 3733, 3734, 3735, 3744, 3746, 3749, 3758, 3763, 3773, 3774, 3775, 3776, 3777, 3778, 3779, 3780, 3781, 3782,
185-
3783, 3784, 3785, 3786, 3787, 3788, 3789, 3790, 3791, 3792, 3793, 3794, 3795, 3796, 3797, 3798, 3799
179+
3561, 3562, 3563, 3564, 3566, 3567, 3568, 3569, 3570, 3572, 3573, 3574, 3575, 3576, 3578, 3579, 3580, 3582, 3584, 3585, 3586, 3587, 3588, 3589,
180+
3590, 3591, 3592, 3593, 3594, 3597, 3598, 3599, 3600, 3601, 3604, 3605, 3609, 3611, 3612, 3613, 3614, 3615, 3617, 3618, 3619, 3620, 3621, 3624,
181+
3626, 3627, 3628, 3629, 3630, 3633, 3634, 3635, 3636, 3637, 3638, 3639, 3640, 3642, 3643, 3644, 3645, 3646, 3648, 3649, 3650, 3651, 3652, 3653,
182+
3654, 3655, 3657, 3658, 3659, 3660, 3661, 3664, 3665, 3666, 3669, 3670, 3671, 3673, 3674, 3675, 3676, 3677, 3679, 3680, 3681, 3684, 3685, 3686,
183+
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
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/3577/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 `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.
9+
* **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.
11+
12+
**1.2 High-level approach:**
13+
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.
15+
16+
![Visualization showing computer 0 as root with arrows to other computers that must have higher complexity]
17+
18+
**1.3 Brute force vs. optimized strategy:**
19+
20+
* **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.
22+
* **Why it's better:** We avoid generating permutations by recognizing the mathematical structure - if the constraint is satisfied, all arrangements are valid.
23+
24+
**1.4 Decomposition:**
25+
26+
1. Check if all computers i > 0 have complexity greater than computer 0.
27+
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+
4. Return the result.
30+
31+
### Steps (The "How")
32+
33+
**2.1 Initialization & Example Setup:**
34+
35+
Let's use the example: `complexity = [1, 2, 3]`
36+
37+
We initialize:
38+
* `MOD = 10^9 + 7` (for modulo arithmetic)
39+
* `n = 3`
40+
* `res = 1` (to compute factorial)
41+
42+
**2.2 Start Checking:**
43+
44+
We iterate through indices from 1 to n-1, checking if each computer can be unlocked.
45+
46+
**2.3 Trace Walkthrough:**
47+
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 |
52+
53+
Since all checks pass, we return 2 (which is (3-1)! = 2!).
54+
55+
**2.4 Increment and Loop:**
56+
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).
58+
59+
**2.5 Return Result:**
60+
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].

solutions/3577/01.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
1-
from typing import List
2-
3-
41
class Solution:
5-
def countPermutations(self, complexity: List[int]) -> int:
2+
def countPermutations(self, complexity: list[int]) -> int:
63
MOD = 10**9 + 7
7-
84
n = len(complexity)
95

10-
# Check if complexity[0] is the unique minimum
11-
min_complexity = complexity[0]
12-
min_count = sum(1 for c in complexity if c == min_complexity)
13-
14-
if min_count > 1:
15-
# If there are other elements with the same complexity as index 0, no valid permutations
16-
return 0
17-
18-
# Index 0 must be first. The remaining n-1 indices can be arranged in any order
19-
# So the answer is (n-1)!
6+
# All computers i > 0 must have complexity[i] > complexity[0]
7+
# If any computer has complexity <= complexity[0], it can never be unlocked
208
res = 1
219
for i in range(1, n):
10+
if complexity[i] <= complexity[0]:
11+
return 0
2212
res = (res * i) % MOD
2313

2414
return res
25-

0 commit comments

Comments
 (0)