Skip to content

Commit 3423ebe

Browse files
LetMeFly666Copilot
andauthored
update: 添加问题“3577.统计计算机解锁顺序排列数”的代码和题解 (#1213)
* word: 2025.12.10(en) * archive * 3577: WA.cpp #1212 * 3577: WA.cpp (#1212) * 3577: AC.cpp+py+go + WA.java (#1212) cpp - AC,100.00%,35.62% py - AC,74.58%,23.73% go - AC,100.00%,25.00% 原来0号机就是第一个,只是要求不同的解锁顺序方式(不是求可行的编号方式) * 3577: AC.java + CE.rust (#1212) java - AC,48.00%,36.00% * 3577: AC.rust (#1212) - AC,100.00%,33.33% * update: 添加问题“3577.统计计算机解锁顺序排列数”的代码和题解 (#1213) Signed-off-by: LetMeFly666 <[email protected]> * Initial plan * Initial plan * Make MOD field final in Java solution Co-authored-by: LetMeFly666 <[email protected]> * Remove compiled class file and update .gitignore Co-authored-by: LetMeFly666 <[email protected]> * Change MOD3577 from var to const in Go file Co-authored-by: LetMeFly666 <[email protected]> * Complete: Changed MOD3577 to const Co-authored-by: LetMeFly666 <[email protected]> * Revert .gitignore changes - not related to this PR Co-authored-by: LetMeFly666 <[email protected]> * Clean up: Remove Go cache file and update .gitignore Co-authored-by: LetMeFly666 <[email protected]> * Fix: Revert formatting changes, keep only var to const change Co-authored-by: LetMeFly666 <[email protected]> * Revert .gitignore changes Co-authored-by: LetMeFly666 <[email protected]> * feat: 1problem1day --------- Signed-off-by: LetMeFly666 <[email protected]> Co-authored-by: copilot-swe-agent[bot] <[email protected]>
1 parent 4fe0f74 commit 3423ebe

14 files changed

+799
-292
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-12-10 22:34:20
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-12-10 22:56:22
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
typedef long long ll;
12+
const ll MOD = 1e9 + 7;
13+
class Solution {
14+
public:
15+
int countPermutations(vector<int>& complexity) {
16+
ll ans = 1;
17+
for (ll i = 1; i < complexity.size(); i++) {
18+
if (complexity[i] <= complexity[0]) {
19+
return 0;
20+
}
21+
ans = ans * i % MOD;
22+
}
23+
return static_cast<int>(ans);
24+
}
25+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-12-10 22:34:20
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-12-10 23:00:19
6+
*/
7+
package main
8+
9+
const MOD3577 int = 1000000007
10+
11+
func countPermutations(complexity []int) int {
12+
ans := 1
13+
for i := 1; i < len(complexity); i++ {
14+
if complexity[i] <= complexity[0] {
15+
return 0
16+
}
17+
ans = ans * i % MOD3577
18+
}
19+
return ans
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-12-10 22:34:20
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-12-10 23:03:09
6+
*/
7+
class Solution {
8+
private final long MOD = 1000000007;
9+
10+
public int countPermutations(int[] complexity) {
11+
long ans = 1;
12+
for (int i = 1; i < complexity.length; i++) {
13+
if (complexity[i] <= complexity[0]) {
14+
return 0;
15+
}
16+
ans = ans * i % MOD;
17+
}
18+
return (int)ans;
19+
}
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-12-10 22:34:20
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-12-10 22:59:13
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
MOD = 1000000007
11+
12+
def countPermutations(self, complexity: List[int]) -> int:
13+
ans = 1
14+
for i in range(1, len(complexity)):
15+
if complexity[i] <= complexity[0]:
16+
return 0
17+
ans = ans * i % self.MOD
18+
return ans
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-12-10 22:34:20
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-12-10 23:06:41
6+
*/
7+
impl Solution {
8+
pub fn count_permutations(complexity: Vec<i32>) -> i32 {
9+
let mut ans: i64 = 1;
10+
let MOD: i64 = 1000000007;
11+
for i in 1..complexity.len() {
12+
if complexity[i] <= complexity[0] {
13+
return 0;
14+
}
15+
ans = ans * (i as i64) % MOD;
16+
}
17+
ans as i32
18+
}
19+
}

Codes/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
* @LastEditTime: 2025-11-01 22:18:35
66
*/
77
pub struct Solution;
8-
include!("1925-count-square-sum-triples.rs"); // 这个fileName是会被脚本替换掉的
8+
include!("3577-count-the-number-of-computer-unlocking-permutations.rs"); // 这个fileName是会被脚本替换掉的

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,7 @@
10531053
|3508.设计路由器|中等|<a href="https://leetcode.cn/problems/implement-router/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/09/20/LeetCode%203508.%E8%AE%BE%E8%AE%A1%E8%B7%AF%E7%94%B1%E5%99%A8/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/151901838" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/implement-router/solutions/3787451/letmefly-3508she-ji-lu-you-qi-stltao-stl-ehkf/" target="_blank">LeetCode题解</a>|
10541054
|3516.找到最近的人|简单|<a href="https://leetcode.cn/problems/find-closest-person/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/09/04/LeetCode%203516.%E6%89%BE%E5%88%B0%E6%9C%80%E8%BF%91%E7%9A%84%E4%BA%BA/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/151184074" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-closest-person/solutions/3772009/letmefly-3516zhao-dao-zui-jin-de-ren-ji-8rlbz/" target="_blank">LeetCode题解</a>|
10551055
|3541.找到频率最高的元音和辅音|简单|<a href="https://leetcode.cn/problems/find-most-frequent-vowel-and-consonant/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/09/13/LeetCode%203541.%E6%89%BE%E5%88%B0%E9%A2%91%E7%8E%87%E6%9C%80%E9%AB%98%E7%9A%84%E5%85%83%E9%9F%B3%E5%92%8C%E8%BE%85%E9%9F%B3/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/151653999" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-most-frequent-vowel-and-consonant/solutions/3780744/letmefly-3541zhao-dao-pin-lu-zui-gao-de-yv5vs/" target="_blank">LeetCode题解</a>|
1056+
|3577.统计计算机解锁顺序排列数|中等|<a href="https://leetcode.cn/problems/count-the-number-of-computer-unlocking-permutations/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/12/10/LeetCode%203577.%E7%BB%9F%E8%AE%A1%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%A7%A3%E9%94%81%E9%A1%BA%E5%BA%8F%E6%8E%92%E5%88%97%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/155791805" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-the-number-of-computer-unlocking-permutations/solutions/3854206/letmefly-3577tong-ji-ji-suan-ji-jie-suo-5b6b8/" target="_blank">LeetCode题解</a>|
10561057
|3583.统计特殊三元组|中等|<a href="https://leetcode.cn/problems/count-special-triplets/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/12/09/LeetCode%203583.%E7%BB%9F%E8%AE%A1%E7%89%B9%E6%AE%8A%E4%B8%89%E5%85%83%E7%BB%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/155754955" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-special-triplets/solutions/3853381/letmefly-3583tong-ji-te-shu-san-yuan-zu-cdnx3/" target="_blank">LeetCode题解</a>|
10571058
|剑指Offer0047.礼物的最大价值|简单|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/03/08/LeetCode%20%E5%89%91%E6%8C%87%20Offer%2047.%20%E7%A4%BC%E7%89%A9%E7%9A%84%E6%9C%80%E5%A4%A7%E4%BB%B7%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/129408765" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/solutions/2155672/letmefly-jian-zhi-offer-47li-wu-de-zui-d-rekb/" target="_blank">LeetCode题解</a>|
10581059
|剑指OfferII0041.滑动窗口的平均值|简单|<a href="https://leetcode.cn/problems/qIsx9U/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/07/16/LeetCode%20%E5%89%91%E6%8C%87%20Offer%20II%200041.%20%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E7%9A%84%E5%B9%B3%E5%9D%87%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125819216" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/qIsx9U/solution/by-tisfy-30mq/" target="_blank">LeetCode题解</a>|
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
---
2+
title: 3577.统计计算机解锁顺序排列数:脑筋急转弯(组合数学)
3+
date: 2025-12-10 23:07:43
4+
tags: [题解, LeetCode, 中等, 脑筋急转弯, 数组, 数学, 组合数学]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】3577.统计计算机解锁顺序排列数:脑筋急转弯(组合数学)
9+
10+
力扣题目链接:[https://leetcode.cn/problems/count-the-number-of-computer-unlocking-permutations/](https://leetcode.cn/problems/count-the-number-of-computer-unlocking-permutations/)
11+
12+
<p>给你一个长度为 <code>n</code> 的数组 <code>complexity</code>。</p>
13+
14+
<p>在房间里有 <code>n</code> 台&nbsp;<strong>上锁的&nbsp;</strong>计算机,这些计算机的编号为 0 到 <code>n - 1</code>,每台计算机都有一个&nbsp;<strong>唯一&nbsp;</strong>的密码。编号为 <code>i</code> 的计算机的密码复杂度为 <code>complexity[i]</code>。</p>
15+
16+
<p>编号为 0 的计算机密码已经&nbsp;<strong>解锁&nbsp;</strong>,并作为根节点。其他所有计算机必须通过它或其他已经解锁的计算机来解锁,具体规则如下:</p>
17+
18+
<ul>
19+
<li>可以使用编号为 <code>j</code> 的计算机的密码解锁编号为 <code>i</code> 的计算机,其中 <code>j</code> 是任何小于 <code>i</code> 的整数,且满足 <code>complexity[j] &lt; complexity[i]</code>(即 <code>j &lt; i</code> 并且 <code>complexity[j] &lt; complexity[i]</code>)。</li>
20+
<li>要解锁编号为 <code>i</code> 的计算机,你需要事先解锁一个编号为 <code>j</code> 的计算机,满足 <code>j &lt; i</code> 并且 <code>complexity[j] &lt; complexity[i]</code>。</li>
21+
</ul>
22+
23+
<p>求共有多少种 <code>[0, 1, 2, ..., (n - 1)]</code> 的排列方式,能够表示从编号为 0 的计算机(唯一初始解锁的计算机)开始解锁所有计算机的有效顺序。</p>
24+
25+
<p>由于答案可能很大,返回结果需要对 <strong>10<sup>9</sup> + 7</strong> 取余数。</p>
26+
27+
<p><strong>注意:</strong>编号为 0 的计算机的密码已解锁,而&nbsp;<strong>不是&nbsp;</strong>排列中第一个位置的计算机密码已解锁。</p>
28+
29+
<p><strong>排列&nbsp;</strong>是一个数组中所有元素的重新排列。</p>
30+
31+
<p>&nbsp;</p>
32+
33+
<p><strong class="example">示例 1:</strong></p>
34+
35+
<div class="example-block">
36+
<p><strong>输入:</strong> <span class="example-io">complexity = [1,2,3]</span></p>
37+
38+
<p><strong>输出:</strong> <span class="example-io">2</span></p>
39+
40+
<p><strong>解释:</strong></p>
41+
42+
<p>有效的排列有:</p>
43+
44+
<ul>
45+
<li>[0, 1, 2]
46+
<ul>
47+
<li>首先使用根密码解锁计算机 0。</li>
48+
<li>使用计算机 0 的密码解锁计算机 1,因为 <code>complexity[0] &lt; complexity[1]</code>。</li>
49+
<li>使用计算机 1 的密码解锁计算机 2,因为 <code>complexity[1] &lt; complexity[2]</code>。</li>
50+
</ul>
51+
</li>
52+
<li>[0, 2, 1]
53+
<ul>
54+
<li>首先使用根密码解锁计算机 0。</li>
55+
<li>使用计算机 0 的密码解锁计算机 2,因为 <code>complexity[0] &lt; complexity[2]</code>。</li>
56+
<li>使用计算机 0 的密码解锁计算机 1,因为 <code>complexity[0] &lt; complexity[1]</code>。</li>
57+
</ul>
58+
</li>
59+
</ul>
60+
</div>
61+
62+
<p><strong class="example">示例 2:</strong></p>
63+
64+
<div class="example-block">
65+
<p><strong>输入:</strong> <span class="example-io">complexity = [3,3,3,4,4,4]</span></p>
66+
67+
<p><strong>输出:</strong> <span class="example-io">0</span></p>
68+
69+
<p><strong>解释:</strong></p>
70+
71+
<p>没有任何排列能够解锁所有计算机。</p>
72+
</div>
73+
74+
<p>&nbsp;</p>
75+
76+
<p><strong>提示:</strong></p>
77+
78+
<ul>
79+
<li><code>2 &lt;= complexity.length &lt;= 10<sup>5</sup></code></li>
80+
<li><code>1 &lt;= complexity[i] &lt;= 10<sup>9</sup></code></li>
81+
</ul>
82+
83+
84+
85+
## 解题方法:组合数学
86+
87+
题目意思是:编号从0到n-1的计算机,编号小且值小的可以解锁编号大且值大的,初始只有编号0(也就是给定序列中的第一个)是解锁状态,问解锁所有计算机有多少种解锁顺序(先解锁1号再解锁2号是一种,先解锁2号再解锁1号是另一种,至于它们是被谁解锁的不重要)。
88+
89+
转念一想发现,如果后面存在小于等于<font title="EVA00">零号机</font>的机器,则无论如何都不可能被解锁;反之若后面值都大于0号机,则后续任何一个机器想在任何次序(th)解锁都可以(都用0号去解锁就好了)。
90+
91+
### 具体方法
92+
93+
遍历一遍数组,如果有值小于等于数组中第一个元素,则返回0。
94+
95+
否则后面$len(complexity)-1$个机器的被解锁顺序可以按任意顺序排列,$(n-1)!$即为所求。
96+
97+
### 时空复杂度分析
98+
99+
+ 时间复杂度$O(n)$,其中$n=len(complexity)$
100+
+ 空间复杂度$O(1)$
101+
102+
### AC代码
103+
104+
#### C++
105+
106+
```cpp
107+
/*
108+
* @LastEditTime: 2025-12-10 22:56:22
109+
*/
110+
typedef long long ll;
111+
const ll MOD = 1e9 + 7;
112+
class Solution {
113+
public:
114+
int countPermutations(vector<int>& complexity) {
115+
ll ans = 1;
116+
for (ll i = 1; i < complexity.size(); i++) {
117+
if (complexity[i] <= complexity[0]) {
118+
return 0;
119+
}
120+
ans = ans * i % MOD;
121+
}
122+
return static_cast<int>(ans);
123+
}
124+
};
125+
```
126+
127+
#### Python
128+
129+
```python
130+
'''
131+
LastEditTime: 2025-12-10 22:59:13
132+
'''
133+
from typing import List
134+
135+
class Solution:
136+
MOD = 1000000007
137+
138+
def countPermutations(self, complexity: List[int]) -> int:
139+
ans = 1
140+
for i in range(1, len(complexity)):
141+
if complexity[i] <= complexity[0]:
142+
return 0
143+
ans = ans * i % self.MOD
144+
return ans
145+
```
146+
147+
#### Java
148+
149+
```java
150+
/*
151+
* @LastEditTime: 2025-12-10 23:03:09
152+
*/
153+
class Solution {
154+
private long MOD = 1000000007;
155+
156+
public int countPermutations(int[] complexity) {
157+
long ans = 1;
158+
for (int i = 1; i < complexity.length; i++) {
159+
if (complexity[i] <= complexity[0]) {
160+
return 0;
161+
}
162+
ans = ans * i % MOD;
163+
}
164+
return (int)ans;
165+
}
166+
}
167+
```
168+
169+
#### Go
170+
171+
```go
172+
/*
173+
* @LastEditTime: 2025-12-10 23:00:19
174+
*/
175+
package main
176+
177+
var MOD3577 int = 1000000007
178+
179+
func countPermutations(complexity []int) int {
180+
ans := 1
181+
for i := 1; i < len(complexity); i++ {
182+
if complexity[i] <= complexity[0] {
183+
return 0
184+
}
185+
ans = ans * i % MOD3577
186+
}
187+
return ans
188+
}
189+
```
190+
191+
#### Rust
192+
193+
```rust
194+
/*
195+
* @LastEditTime: 2025-12-10 23:06:41
196+
*/
197+
impl Solution {
198+
pub fn count_permutations(complexity: Vec<i32>) -> i32 {
199+
let mut ans: i64 = 1;
200+
let MOD: i64 = 1000000007;
201+
for i in 1..complexity.len() {
202+
if complexity[i] <= complexity[0] {
203+
return 0;
204+
}
205+
ans = ans * (i as i64) % MOD;
206+
}
207+
ans as i32
208+
}
209+
}
210+
```
211+
212+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/155791805)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/12/10/LeetCode%203577.%E7%BB%9F%E8%AE%A1%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%A7%A3%E9%94%81%E9%A1%BA%E5%BA%8F%E6%8E%92%E5%88%97%E6%95%B0/)哦~
213+
>
214+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,7 @@ categories: [自用]
11071107
|stammer|v. 口吃,结结巴巴地说<br>n. 口吃,结巴|
11081108
|||
11091109
|unjust|adj. 不公平的,不公正的,非正义的|
1110-
|handkerchief|n. 手帕,纸巾|
1110+
|<font color="#28bea0" title="二次复习">handkerchief</font>|n. 手帕,纸巾|
11111111
|clientele|n. 客户|
11121112
|||
11131113
|terrace|n. 露天平台,阳台,阶梯看台,梯田|

r.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)