Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 7a8fc42

Browse files
aQuaaQua
authored andcommitted
649 finish
1 parent dc18ab2 commit 7a8fc42

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed
Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
11
package Problem0649
22

3-
var result = map[byte]string{
4-
'R': "Radiant",
5-
'D': "Dire",
6-
}
7-
83
func predictPartyVictory(senate string) string {
9-
bs := make([]byte, 0, len(senate)*2)
10-
bs = append(bs, []byte(senate)...)
11-
return helper(bs)
12-
}
4+
n := len(senate)
135

14-
func helper(bs []byte) string {
15-
b := bs[0]
16-
var i int
17-
for i = 1; i < len(bs); i++ {
18-
if bs[i] != b {
19-
break
6+
// qr 和 qd 按顺序保存了 r 和 d 的议员的索引号
7+
qr := make([]int, 0, n)
8+
qd := make([]int, 0, n)
9+
for i, b := range senate {
10+
if b == 'R' {
11+
qr = append(qr, i)
12+
} else {
13+
qd = append(qd, i)
2014
}
2115
}
2216

23-
if i == len(bs) {
24-
return result[b]
17+
// 对议员来说,最优策略就是,把下一个对方党派的议员 ban 掉
18+
for len(qr) > 0 && len(qd) > 0 {
19+
ri := qr[0]
20+
qr = qr[1:]
21+
di := qd[0]
22+
qd = qd[1:]
23+
if ri < di {
24+
// ri ban 掉了 di
25+
// ri 的索引号 +n
26+
qr = append(qr, ri+n)
27+
} else {
28+
qd = append(qd, di+n)
29+
}
2530
}
2631

27-
copy(bs[i:], bs[i+1:])
28-
bs[len(bs)-1] = b
29-
return helper(bs[1:])
32+
if len(qr) > 0 {
33+
return "Radiant"
34+
}
35+
return "Dire"
3036
}

Algorithms/0649.dota2-senate/dota2-senate_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ var tcs = []struct {
1313
ans string
1414
}{
1515

16+
{
17+
"RRDDD",
18+
"Radiant",
19+
},
20+
1621
{
1722
"DDRRR",
1823
"Dire",

0 commit comments

Comments
 (0)