LeetCode Challenge Day 86 β 3577. Count the Number of Computer Unlocking Permutations
Nitin Ahirwal / December 10, 2025
Hey folks π
This is Day 86 of my LeetCode streak π
Today's problem is 3577. Count the Number of Computer Unlocking Permutations β a medium problem that rewards spotting a crucial constraint early.
π Problem Statement
You are given an array complexity of length n.
There are n locked computers labeled from 0 to n - 1.
Computer 0 is already unlocked and acts as the root.
A computer i can be unlocked using a previously unlocked computer j if:
j < icomplexity[j] < complexity[i]
Your task is to count how many permutations of [0, 1, 2, ..., n - 1] represent a valid unlocking order.
Return the result modulo 10βΉ + 7.
π‘ Intuition
The most important computer is the root: 0.
For every other computer i > 0, there must exist some computer before it with:
- a smaller index
- lower complexity
- already unlocked
If computer 0 is not strictly smaller than all other complexities, then at least one computer will never find a valid parent β making the answer 0.
But if complexity[0] is the unique global minimum, then:
- Computer
0can unlock any other computer directly - The remaining computers can be unlocked in any order
Thatβs the key insight.
π Approach
-
Check whether
complexity[0]is the strict smallest element in the array.- If any
complexity[i] β€ complexity[0]fori > 0, return0.
- If any
-
If the condition holds:
- Computer
0must be unlocked first - The remaining
n - 1computers can be unlocked in any permutation
- Computer
-
The total number of valid unlocking orders becomes:
(n - 1)!
-
Compute the factorial modulo
10βΉ + 7.
BigInt is used in JavaScript to safely handle large factorial values.
β±οΈ Complexity Analysis
-
Time Complexity:
O(n)β One pass for validation and one loop for factorial computation. -
Space Complexity:
O(1)β Constant extra space.
π§βπ» Code (JavaScript)
/**
- @param {number[]} complexity
- @return {number}
*/
var countPermutations = function(complexity) {
const MOD = 1000000007n;
const n = complexity.length;
// Check that complexity[0] is the unique global minimum
for (let i = 1; i < n; i++) {
if (complexity[i] <= complexity[0]) {
return 0;
}
}
// Compute (n - 1)! % MOD using BigInt
let res = 1n;
for (let i = 2n; i <= BigInt(n - 1); i++) {
res = (res * i) % MOD;
}
return Number(res);
};
π― Reflection
This problem is a great reminder that:
- A single constraint can collapse a complex permutation problem into a simple formula
- Validating impossibility early saves unnecessary computation
- Sometimes, the entire problem hinges on one special element
Thatβs it for Day 86 of my LeetCode challenge πͺ
Streak continues π₯
Happy Coding π¨βπ»