Count the Number of Computer Unlocking Permutations - Problem

You're a cybersecurity expert who needs to unlock a series of encrypted computers in a secure facility. Each computer has a complexity rating for its password, and there's a specific hierarchy for unlocking them.

The Rules:

  • Computer 0 is already unlocked (your starting point)
  • To unlock computer i, you must use a previously unlocked computer j where:
    • j < i (lower labeled computer)
    • complexity[j] < complexity[i] (simpler password helps unlock more complex one)

Goal: Count how many different valid sequences exist for unlocking all computers from 0 to n-1.

Example: If you have computers [0,1,2] with complexities [1,3,2], you could unlock them as [0,2,1] because computer 0 (complexity 1) can unlock computer 2 (complexity 2), and then computer 2 can unlock computer 1 (complexity 3).

Return the answer modulo 10^9 + 7.

Input & Output

example_1.py โ€” Basic Case
$ Input: [1, 3, 2]
โ€บ Output: 1
๐Ÿ’ก Note: Computers have complexities [1,3,2]. Computer 0 (complexity 1) is unlocked first. Only valid sequence: unlock computer 2 (complexity 2) using computer 0, then unlock computer 1 (complexity 3) using computer 2. This gives us permutation [0,2,1].
example_2.py โ€” Multiple Valid Sequences
$ Input: [1, 2, 3]
โ€บ Output: 2
๐Ÿ’ก Note: Computer 0 has complexity 1. Valid sequences: [0,1,2] (unlock 1 with 0, then 2 with 1) and [0,2,1] (unlock 2 with 0, then 1 with 2). Both work because each step follows the complexity constraint.
example_3.py โ€” Single Computer
$ Input: [5]
โ€บ Output: 1
๐Ÿ’ก Note: Only one computer exists (computer 0), so there's exactly one way to 'unlock' all computers: [0].

Visualization

Tap to expand
Computer Unlocking Process0unlockedComplexity: 11lockedComplexity: 32lockedComplexity: 21 < 3 โœ“1 < 2 โœ“Valid Unlocking Sequence: [0, 2, 1]Step 1: 0Initially unlockedStep 2: 2Unlocked by 0Step 3: 1Unlocked by 2
Understanding the Visualization
1
Initial State
Computer 0 is unlocked and ready to unlock others
2
Find Candidates
Identify which computers can be unlocked next based on complexity rules
3
Apply Constraints
Ensure each computer can only unlock those with higher complexity and ID
4
Count Arrangements
Calculate total valid permutations using mathematical relationships
Key Takeaway
๐ŸŽฏ Key Insight: The problem reduces to counting arrangements where each computer can be unlocked by a previously placed computer with both lower complexity AND lower ID number.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

Sorting the complexity array dominates, DP is O(n)

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space for sorted array and DP table

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค n โ‰ค 1000
  • 1 โ‰ค complexity[i] โ‰ค 106
  • All complexity values are positive integers
  • Computer 0 is always initially unlocked
Asked in
Google 45 Microsoft 38 Amazon 32 Meta 25
43.4K Views
Medium-High Frequency
~25 min Avg. Time
1.8K 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