|
| 1 | +# [Problem 3577: Count the Number of Computer Unlocking Permutations](https://leetcode.com/problems/count-the-number-of-computer-unlocking-permutations/description/?envType=daily-question) |
| 2 | + |
| 3 | +## Initial thoughts (stream-of-consciousness) |
| 4 | +We are given an array complexity of length n and asked how many permutations of [0..n-1] represent valid unlocking orders starting from computer 0 being already decrypted (the root). |
| 5 | +The unlocking rule for computer i is: at the moment you try to unlock i, there must already be some unlocked computer j with j < i and complexity[j] < complexity[i]. |
| 6 | + |
| 7 | +I need to carefully interpret the rule: j is a label (an integer less than i), and j must be among already unlocked computers. Computer 0 is initially unlocked, so for any i>0, j = 0 is a candidate provided complexity[0] < complexity[i]. If complexity[0] is strictly smaller than complexity[i], then 0 can always unlock i at any time (because 0 < i always and 0 is unlocked from the start). If complexity[0] is not strictly smaller than some i (equal or larger), then that i cannot be unlocked by 0; it would require some other j < i with smaller complexity, but if no such j exists among labels < i then i can never be unlocked. |
| 8 | + |
| 9 | +This suggests a simple necessary condition: for every i > 0 there must exist some k < i with complexity[k] < complexity[i]. In particular for i = 1 this requires complexity[0] < complexity[1]. For i = 2 this requires min(complexity[0], complexity[1]) < complexity[2], etc. Observing the chain, if complexity[0] is not strictly the global minimum (i.e., there's some value <= complexity[0] elsewhere), then there will be some index that cannot be unlocked. So I suspect complexity[0] must be the unique minimum in the whole array. If it is the unique minimum, then 0 can unlock every other node immediately, so after placing 0 first all remaining nodes are available in any order, giving (n-1)! permutations (with 0 first). |
| 10 | + |
| 11 | +I should double-check examples and edge cases (duplicates of minimum, etc.). |
| 12 | + |
| 13 | +## Refining the problem, round 2 thoughts |
| 14 | +- If complexity[0] is not strictly smaller than all other complexities, there exists some index i>0 such that no j<i has complexity[j] < complexity[i] (in particular if complexity[i] <= complexity[0] and all earlier ones >= complexity[i]) — that i can never be unlocked. So answer 0 in that case. |
| 15 | +- If complexity[0] is the unique global minimum, then for every i>0 we have complexity[0] < complexity[i] and 0 < i, so 0 can unlock any i at any time. Once 0 is placed (the initial unlocked computer, logically first in unlocking order), all remaining n-1 computers are simultaneously available and can be unlocked in any permutation: (n-1)! ways. |
| 16 | +- Hence the solution becomes: check if complexity[0] is the unique smallest element in the array. If not, return 0. Otherwise return factorial(n-1) modulo 1e9+7. |
| 17 | +- Time complexity: O(n) to find min and count and O(n) to compute factorial up to n-1 (or O(1) with precomputation if cached). Space O(1). |
| 18 | + |
| 19 | +This reasoning matches the provided examples: [1,2,3] -> complexity[0] unique min -> (3-1)! = 2. [3,3,3,4,4,4] -> complexity[0] not unique min (ties) -> 0. |
| 20 | + |
| 21 | +## Attempted solution(s) |
| 22 | +```python |
| 23 | +from typing import List |
| 24 | + |
| 25 | +class Solution: |
| 26 | + def countPermutations(self, complexity: List[int]) -> int: |
| 27 | + MOD = 10**9 + 7 |
| 28 | + n = len(complexity) |
| 29 | + # Check if complexity[0] is the unique global minimum |
| 30 | + min_val = min(complexity) |
| 31 | + if complexity[0] != min_val: |
| 32 | + return 0 |
| 33 | + # If the minimum occurs more than once, 0 cannot be unlocked by 0 (strict < required) |
| 34 | + if complexity.count(min_val) != 1: |
| 35 | + return 0 |
| 36 | + # Otherwise, 0 can unlock any other computer, so the remaining (n-1) can be ordered arbitrarily |
| 37 | + res = 1 |
| 38 | + for i in range(2, n): # multiply 2 * 3 * ... * (n-1) |
| 39 | + res = (res * i) % MOD |
| 40 | + return res |
| 41 | +``` |
| 42 | +- Notes on the solution: |
| 43 | + - We check that complexity[0] equals the minimum and that the minimum is unique. If either condition fails, return 0. |
| 44 | + - If the condition holds, the number of valid permutations is (n-1)! modulo 1e9+7 because after the (implicit) first unlocked computer 0, all other n-1 computers can be unlocked in any order (0 can directly unlock each of them). |
| 45 | + - Time complexity: O(n) for finding min, counting occurrences, and computing factorial up to n-1 (overall O(n)). Space complexity: O(1) extra space. |
0 commit comments