This repository was archived by the owner on Sep 20, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +31
-20
lines changed
Algorithms/0649.dota2-senate Expand file tree Collapse file tree 2 files changed +31
-20
lines changed Original file line number Diff line number Diff line change 1
1
package Problem0649
2
2
3
- var result = map [byte ]string {
4
- 'R' : "Radiant" ,
5
- 'D' : "Dire" ,
6
- }
7
-
8
3
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 )
13
5
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 )
20
14
}
21
15
}
22
16
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
+ }
25
30
}
26
31
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"
30
36
}
Original file line number Diff line number Diff line change @@ -13,6 +13,11 @@ var tcs = []struct {
13
13
ans string
14
14
}{
15
15
16
+ {
17
+ "RRDDD" ,
18
+ "Radiant" ,
19
+ },
20
+
16
21
{
17
22
"DDRRR" ,
18
23
"Dire" ,
You can’t perform that action at this time.
0 commit comments