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 Permutations INPUT PC 0 c=1 [ROOT] PC 1 c=3 [LOCKED] PC 2 c=2 [LOCKED] complexity array: 1 3 2 i=0 i=1 i=2 Unlock Rule: j < i AND complexity[j] < complexity[i] PC1 needs: j<1, c[j]<3 PC2 needs: j<2, c[j]<2 ALGORITHM STEPS 1 Build dependency graph Find valid j for each i 2 DP with memoization Track unlocked state as bitmask 3 Enumerate permutations Count valid unlock orders 4 Return count mod 10^9+7 Sum all valid paths Dependencies: 0 2 1 0 unlocks 2 (1<2), then 2 unlocks 1 FINAL RESULT Only Valid Permutation: 0 --> 2 --> 1 Step 1: Start with PC 0 (root) Step 2: Unlock PC 2 (0<2, 1<2) Step 3: Unlock PC 1 (0<1, 1<3) Why not [0, 1, 2]? PC 2 needs j<2 with c[j]<2 Only PC 0 works (c[0]=1<2) OUTPUT 1 OK - One valid permutation Key Insight: DP with bitmask memoization efficiently counts valid unlock sequences. Each computer i can only be unlocked by a computer j where j < i and complexity[j] < complexity[i]. This creates a DAG structure where we count topological orderings starting from node 0. Time: O(n! * n) worst case, optimized with memo. TutorialsPoint - Count the Number of Computer Unlocking Permutations | Dynamic Programming with Memoization
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