Skip to content

Commit 49de4f6

Browse files
committed
[E:56/531, M:53/969, H:6/386] add No: 649 Dota2 Senate
1 parent 0ccf8be commit 49de4f6

File tree

9 files changed

+321
-1
lines changed

9 files changed

+321
-1
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
## [Dota2 参议院](https://leetcode-cn.com/problems/dota2-senate/)
2+
3+
Dota2 的世界里有两个阵营:`Radiant`(天辉)和 `Dire`(夜魇)
4+
5+
Dota2 参议院由来自两派的参议员组成。现在参议院希望对一个 Dota2 游戏里的改变作出决定。他们以一个基于轮为过程的投票进行。在每一轮中,每一位参议员都可以行使两项权利中的`**一**`项:
6+
7+
1.
8+
9+
`禁止一名参议员的权利`
10+
11+
参议员可以让另一位参议员在这一轮和随后的几轮中丧失**所有的权利**
12+
13+
2.
14+
15+
`宣布胜利`
16+
17+
          如果参议员发现有权利投票的参议员都是**同一个阵营的**,他可以宣布胜利并决定在游戏中的有关变化。
18+
19+
 
20+
21+
给定一个字符串代表每个参议员的阵营。字母 “R” 和 “D” 分别代表了 `Radiant`(天辉)和 `Dire`(夜魇)。然后,如果有 `n` 个参议员,给定字符串的大小将是 `n`
22+
23+
以轮为基础的过程从给定顺序的第一个参议员开始到最后一个参议员结束。这一过程将持续到投票结束。所有失去权利的参议员将在过程中被跳过。
24+
25+
假设每一位参议员都足够聪明,会为自己的政党做出最好的策略,你需要预测哪一方最终会宣布胜利并在 Dota2 游戏中决定改变。输出应该是 `Radiant` 或 `Dire`
26+
27+
 
28+
29+
**示例 1:**
30+
31+
`
32+
**输入:**"RD"
33+
**输出:**"Radiant"
34+
**解释:**`第一个参议员来自 Radiant 阵营并且他可以使用第一项权利让第二个参议员失去权力,因此第二个参议员将被跳过因为他没有任何权利。然后在第二轮的时候,第一个参议员可以宣布胜利,因为他是唯一一个有投票权的人`
35+
`
36+
37+
**示例 2:**
38+
39+
`
40+
**输入:**"RDD"
41+
**输出:**"Dire"
42+
**解释:**
43+
第一轮中,第一个`来自 Radiant 阵营的`参议员可以使用第一项权利禁止第二个参议员的权利
44+
第二个`来自 Dire 阵营的`参议员会被跳过因为他的权利被禁止
45+
第三个`来自 Dire 阵营的`参议员可以使用他的第一项权利禁止第一个参议员的权利
46+
因此在第二轮只剩下第三个参议员拥有投票的权利,于是他可以宣布胜利
47+
`
48+
49+
 
50+
51+
**提示:**
52+
53+
* 给定字符串的长度在 `[1, 10,000]` 之间.
54+
55+
 
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/中等/649/golang/solution"
11+
)
12+
13+
func main() {
14+
15+
tests := []struct {
16+
name string
17+
input string
18+
want string
19+
}{
20+
{
21+
name: "test-RD",
22+
input: "RD",
23+
want: "Radiant",
24+
},
25+
{
26+
name: "test-RDD",
27+
input: "RDD",
28+
want: "Dire",
29+
},
30+
{
31+
name: "test-RDDD",
32+
input: "RDDD",
33+
want: "Dire",
34+
},
35+
{
36+
name: "test-RDDR",
37+
input: "RDDR",
38+
want: "Radiant",
39+
},
40+
{
41+
name: "test-RRDDD",
42+
input: "RRDDD",
43+
want: "Radiant",
44+
},
45+
{
46+
name: "test-DRRDRDRDRDDRDRDR",
47+
input: "DRRDRDRDRDDRDRDR",
48+
want: "Radiant",
49+
},
50+
}
51+
52+
testLog := leet.NewTestLog(len(tests))
53+
defer testLog.Render()
54+
55+
timeoutDuration := time.Second * 2
56+
57+
for idx, test := range tests {
58+
// 超时检测
59+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
60+
solution.Export(test.input)
61+
cancel()
62+
})
63+
64+
if timeout {
65+
testLog.Fail(idx+1, test.name, "timeout")
66+
continue
67+
}
68+
69+
got := solution.Export(test.input)
70+
if !reflect.DeepEqual(test.want, got) {
71+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
72+
continue
73+
}
74+
75+
testLog.Pass(idx+1, test.name)
76+
}
77+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(senate string) string {
4+
return predictPartyVictory(senate)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func predictPartyVictory(senate string) string {
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(senate string) string {
4+
return predictPartyVictory(senate)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func predictPartyVictory(senate string) string {
13+
14+
}

questions/store.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
## [Dota2 参议院](https://leetcode-cn.com/problems/dota2-senate/)
2+
3+
Dota2 的世界里有两个阵营:`Radiant`(天辉)和 `Dire`(夜魇)
4+
5+
Dota2 参议院由来自两派的参议员组成。现在参议院希望对一个 Dota2 游戏里的改变作出决定。他们以一个基于轮为过程的投票进行。在每一轮中,每一位参议员都可以行使两项权利中的`**一**`项:
6+
7+
1.
8+
9+
`禁止一名参议员的权利`
10+
11+
参议员可以让另一位参议员在这一轮和随后的几轮中丧失**所有的权利**
12+
13+
2.
14+
15+
`宣布胜利`
16+
17+
          如果参议员发现有权利投票的参议员都是**同一个阵营的**,他可以宣布胜利并决定在游戏中的有关变化。
18+
19+
 
20+
21+
给定一个字符串代表每个参议员的阵营。字母 “R” 和 “D” 分别代表了 `Radiant`(天辉)和 `Dire`(夜魇)。然后,如果有 `n` 个参议员,给定字符串的大小将是 `n`
22+
23+
以轮为基础的过程从给定顺序的第一个参议员开始到最后一个参议员结束。这一过程将持续到投票结束。所有失去权利的参议员将在过程中被跳过。
24+
25+
假设每一位参议员都足够聪明,会为自己的政党做出最好的策略,你需要预测哪一方最终会宣布胜利并在 Dota2 游戏中决定改变。输出应该是 `Radiant` 或 `Dire`
26+
27+
 
28+
29+
**示例 1:**
30+
31+
`
32+
**输入:**"RD"
33+
**输出:**"Radiant"
34+
**解释:**`第一个参议员来自 Radiant 阵营并且他可以使用第一项权利让第二个参议员失去权力,因此第二个参议员将被跳过因为他没有任何权利。然后在第二轮的时候,第一个参议员可以宣布胜利,因为他是唯一一个有投票权的人`
35+
`
36+
37+
**示例 2:**
38+
39+
`
40+
**输入:**"RDD"
41+
**输出:**"Dire"
42+
**解释:**
43+
第一轮中,第一个`来自 Radiant 阵营的`参议员可以使用第一项权利禁止第二个参议员的权利
44+
第二个`来自 Dire 阵营的`参议员会被跳过因为他的权利被禁止
45+
第三个`来自 Dire 阵营的`参议员可以使用他的第一项权利禁止第一个参议员的权利
46+
因此在第二轮只剩下第三个参议员拥有投票的权利,于是他可以宣布胜利
47+
`
48+
49+
 
50+
51+
**提示:**
52+
53+
* 给定字符串的长度在 `[1, 10,000]` 之间.
54+
55+
 
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/中等/649/golang/solution"
11+
)
12+
13+
func main() {
14+
15+
tests := []struct {
16+
name string
17+
input string
18+
want string
19+
}{
20+
{
21+
name: "test-RD",
22+
input: "RD",
23+
want: "Radiant",
24+
},
25+
{
26+
name: "test-RDD",
27+
input: "RDD",
28+
want: "Dire",
29+
},
30+
{
31+
name: "test-RDDD",
32+
input: "RDDD",
33+
want: "Dire",
34+
},
35+
{
36+
name: "test-RDDR",
37+
input: "RDDR",
38+
want: "Radiant",
39+
},
40+
{
41+
name: "test-RRDDD",
42+
input: "RRDDD",
43+
want: "Radiant",
44+
},
45+
{
46+
name: "test-DRRDRDRDRDDRDRDR",
47+
input: "DRRDRDRDRDDRDRDR",
48+
want: "Radiant",
49+
},
50+
}
51+
52+
testLog := leet.NewTestLog(len(tests))
53+
defer testLog.Render()
54+
55+
timeoutDuration := time.Second * 2
56+
57+
for idx, test := range tests {
58+
// 超时检测
59+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
60+
solution.Export(test.input)
61+
cancel()
62+
})
63+
64+
if timeout {
65+
testLog.Fail(idx+1, test.name, "timeout")
66+
continue
67+
}
68+
69+
got := solution.Export(test.input)
70+
if !reflect.DeepEqual(test.want, got) {
71+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
72+
continue
73+
}
74+
75+
testLog.Pass(idx+1, test.name)
76+
}
77+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(senate string) string {
4+
return predictPartyVictory(senate)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func predictPartyVictory(senate string) string {
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(senate string) string {
4+
return predictPartyVictory(senate)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func predictPartyVictory(senate string) string {
13+
14+
}

0 commit comments

Comments
 (0)