Count the Number of Computer Unlocking Permutations

Medium
Watch on YouTube ↗

Solution

class Solution {
    int mod = 1_000_000_007; 
    public int countPermutations(int[] complexity) {
        
        int first = complexity[0];
        // making sure that we are able to unlock all computers
        for(int i=1; i<complexity.length; i++) {
            if(complexity[i] <= first) {
                return 0;
            }
        }
        // No of ways to place n-1 computers in n-1 places
        return fact(complexity.length-1);

    }

    int fact(int n) {
        long res = 1;
        for(int i=2; i<=n; i++) {
            res = (res*i)%mod;
        }

        return (int)res;
    }
}