Count the Number of Computer Unlocking Permutations - Problem

You are given an array complexity of length n. There are n locked computers in a room with labels from 0 to n - 1, each with its own unique password. The password of computer i has complexity complexity[i].

The password for computer labeled 0 is already decrypted and serves as the root. All other computers must be unlocked using it or another previously unlocked computer, following this rule:

You can decrypt the password for computer i using the password for computer j, where j is any integer less than i with a lower complexity (i.e., j < i and complexity[j] < complexity[i]).

Find the number of permutations of [0, 1, 2, ..., (n - 1)] that represent a valid order in which the computers can be unlocked, starting from computer 0 as the only initially unlocked one.

Since the answer may be large, return it modulo 10^9 + 7.

Note: The password for computer with label 0 is decrypted, not the computer with the first position in the permutation.

Input & Output

Example 1 — Basic Case
$ Input: complexity = [1,3,2]
Output: 1
💡 Note: Only one valid permutation: [0,1,2]. Computer 0 (complexity 1) unlocks first, then computer 1 (complexity 3) can be unlocked using 0, then computer 2 (complexity 2) can be unlocked using 0.
Example 2 — Multiple Valid Sequences
$ Input: complexity = [1,2,3,4]
Output: 1
💡 Note: Only [0,1,2,3] is valid since each computer i can only be unlocked by j where j < i and complexity[j] < complexity[i].
Example 3 — Edge Case
$ Input: complexity = [1,1]
Output: 0
💡 Note: Computer 1 cannot be unlocked since complexity[0] = complexity[1] = 1, but we need complexity[0] < complexity[1].

Constraints

  • 1 ≤ complexity.length ≤ 20
  • 1 ≤ complexity[i] ≤ 105

Visualization

Tap to expand
Computer Unlocking: Find Valid Permutation Count0Complexity11Complexity32Complexity2✓ UnlockedLockedLockedCan unlockCan unlockValid Sequence: [0, 1, 2] or [0, 2, 1]?Only [0, 1, 2] works: 0→1 (1<3), 0→2 (1<2)[0, 2, 1] fails: 2→1 needs jResult: 1 valid permutation
Understanding the Visualization
1
Input
Array of complexity values [1,3,2]
2
Process
Find valid unlocking sequences where j < i and complexity[j] < complexity[i]
3
Output
Count of valid permutations
Key Takeaway
🎯 Key Insight: Computer i can only be unlocked by computer j where j < i and complexity[j] < complexity[i]
Asked in
Google 25 Facebook 18 Microsoft 15
8.4K Views
Medium Frequency
~35 min Avg. Time
186 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen