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
0is already unlocked (your starting point) - To unlock computer
i, you must use a previously unlocked computerjwhere: 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
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)
โก Linearithmic
Space Complexity
O(n)
Space for sorted array and DP table
โก Linearithmic Space
Constraints
- 1 โค n โค 1000
- 1 โค complexity[i] โค 106
- All complexity values are positive integers
- Computer 0 is always initially unlocked
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code