Leetcode 3577. Count the Number of Computer Unlocking Permutations
- Leetcode 3577. Count the Number of Computer Unlocking Permutations
- 1. 解题思路
- 2. 代码实现
- 题目链接:3577. Count the Number of Computer Unlocking Permutations
1. 解题思路
这一题其实就是一个脑筋急转弯,要想要能够将所有的电脑解锁,那么第一个元素必须是最小的,否则就直接返回0即可。
而如果第一个元素就是最小的,那么剩下的情况就是一个排列的问题,答案就是 ( n − 1 ) ! (n-1)! (n−1)!,我们直接返回即可。
2. 代码实现
给出python代码实现如下:
MOD = 10**9+7class Solution:def countPermutations(self, complexity: List[int]) -> int:n = len(complexity)if any(x <= complexity[0] for x in complexity[1:]):return 0ans = 1for i in range(1, n):ans = (ans * i) % MODreturn ans
提交代码评测得到:耗时25ms,占用内存31.8MB。