#3577
Medium Algorithms Count the number of computer unlocking permutations
Array Math Brainteaser Combinatorics
59.0% acceptance
Feb 25, 2026
355
129
You are given an array complexity. Computer 0 is unlocked (root). Computer i can be
unlocked using any already-unlocked j with j < i and complexity[j] < complexity[i].
Count valid permutations of [0..n-1] to unlock all computers. Answer mod 10^9+7.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn count_permutations(complexity: Vec<i32>) -> i32 {
const MOD: i64 = 1_000_000_007;
let n = complexity.len();
// Computer 0 must be first (it's the root). For i>0, computer i needs
// some j<i with complexity[j]<complexity[i] to already be unlocked.
// Key observation: complexity[0] must be strictly less than all other complexities.
// If any complexity[i] <= complexity[0] for i>0, answer is 0.
// If all complexity[i] > complexity[0] for i>0, then any permutation of [1..n-1]
// is valid (computer 0 can unlock any of them in any order).
// Wait: computer i also needs complexity[j] < complexity[i] where j is already unlocked.
// Starting with just {0}: any i with complexity[i] > complexity[0] can be unlocked.
// So we can unlock all as long as complexity[0] < complexity[i] for all i>0.
// The number of valid orderings = (n-1)! if complexity[0] < min(complexity[1..])
// But what if some later computers have equal complexity? They still need j < i,
// but the index constraint (j < i in original labels) matters.
// Actually re-reading: "j < i" means j's label is less than i's label in original.
// For computer i, we need some already-unlocked computer j with j < i, complexity[j] < complexity[i].
// Computer 0 is unlocked initially.
// For permutation ordering: computer 0 must appear first in permutation (it's root).
// Then computer i can appear at any position AFTER at least one valid j (j<i, comp[j]<comp[i])
// has appeared before it.
// The number of valid permutations: position computer 0 first, then count orderings of rest.
// For the rest [1..n-1], we need each i to have some j<i (label) with comp[j]<comp[i]
// appearing before i in the permutation.
// Since comp[0] must unlock at least computer 1 (or some chain must work),
// the simplest sufficient condition: each i>0 must have comp[0] < comp[i] or some j with j<i, comp[j]<comp[i].
//
// If all complexity[i] > complexity[0] for i in 1..n: every computer can be directly unlocked by 0.
// Then any of the (n-1)! orderings of [1..n-1] work.
//
// If some complexity[i] <= complexity[0] for i>0: computer i cannot be unlocked by 0.
// It needs another j < i with comp[j] < comp[i].
// This is complex in general. But the problem has simple constraints that lead to:
// - If complexity[0] is NOT strictly the minimum, answer is 0.
// - If complexity[0] IS strictly less than all others, answer is (n-1)!.
// This matches both examples.
let c0 = complexity[0];
for i in 1..n {
if complexity[i] <= c0 {
return 0;
}
}
// Answer is (n-1)!
let mut ans: i64 = 1;
for i in 1..n as i64 {
ans = ans * i % MOD;
}
ans as i32
}
}