Skip to content

Commit 410c835

Browse files
committed
Add solution and test-cases for problem 3577
1 parent 7ce34f6 commit 410c835

File tree

3 files changed

+58
-22
lines changed

3 files changed

+58
-22
lines changed

leetcode/3501-3600/3577.Count-the-Number-of-Computer-Unlocking-Permutations/README.md

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,53 @@
11
# [3577.Count the Number of Computer Unlocking Permutations][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given an array `complexity` of length `n`.
5+
6+
There are `n` **locked** computers in a room with labels from 0 to `n - 1`, each with its own **unique** password. The password of the computer `i` has a `complexity complexity[i]`.
7+
8+
The password for the computer labeled 0 is **already** decrypted and serves as the root. All other computers must be unlocked using it or another previously unlocked computer, following this information:
9+
10+
- You can decrypt the password for the computer `i` using the password for computer `j`, where `j` is **any** integer less than `i` with a lower complexity. (i.e. `j < i` and `complexity[j] < complexity[i]`)
11+
- To decrypt the password for computer `i`, you must have already unlocked a computer `j` such that `j < i` and `complexity[j] < complexity[i]`.
12+
13+
Find the number of permutations of `[0, 1, 2, ..., (n - 1)]` that represent a valid order in which the computers can be unlocked, starting from computer 0 as the only initially unlocked one.
14+
15+
Since the answer may be large, return it **modulo** `10^9 + 7`.
16+
17+
**Note** that the password for the computer **with label** 0 is decrypted, and not the computer with the first position in the permutation.
718

819
**Example 1:**
920

1021
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
22+
Input: complexity = [1,2,3]
1423
15-
## 题意
16-
> ...
24+
Output: 2
1725
18-
## 题解
26+
Explanation:
27+
28+
The valid permutations are:
29+
30+
[0, 1, 2]
31+
Unlock computer 0 first with root password.
32+
Unlock computer 1 with password of computer 0 since complexity[0] < complexity[1].
33+
Unlock computer 2 with password of computer 1 since complexity[1] < complexity[2].
34+
[0, 2, 1]
35+
Unlock computer 0 first with root password.
36+
Unlock computer 2 with password of computer 0 since complexity[0] < complexity[2].
37+
Unlock computer 1 with password of computer 0 since complexity[0] < complexity[1].
38+
```
39+
40+
**Example 2:**
1941

20-
### 思路1
21-
> ...
22-
Count the Number of Computer Unlocking Permutations
23-
```go
2442
```
43+
Input: complexity = [3,3,3,4,4,4]
2544
45+
Output: 0
46+
47+
Explanation:
48+
49+
There are no possible permutations which can unlock all computers.
50+
```
2651

2752
## 结语
2853

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(complexity []int) int {
4+
n := len(complexity)
5+
for i := 1; i < n; i++ {
6+
if complexity[i] <= complexity[0] {
7+
return 0
8+
}
9+
}
10+
11+
ans := 1
12+
mod := 1000000007
13+
for i := 2; i < n; i++ {
14+
ans = ans * i % mod
15+
}
16+
return ans
517
}

leetcode/3501-3600/3577.Count-the-Number-of-Computer-Unlocking-Permutations/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{1, 2, 3}, 2},
17+
{"TestCase2", []int{3, 3, 3, 4, 4, 4}, 0},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)