File tree 1 file changed +76
-0
lines changed
1 file changed +76
-0
lines changed Original file line number Diff line number Diff line change
1
+ from collections import deque
2
+
3
+
4
+ class Radiant :
5
+ name = "Radiant"
6
+ mark = "R"
7
+
8
+
9
+ class Dire :
10
+ name = "Dire"
11
+ mark = "D"
12
+
13
+
14
+ class Solution :
15
+ def predictPartyVictory (self , senate : str ) -> str :
16
+ queue = deque (senate )
17
+ radiant_banned , dire_banned = 0 , 0
18
+ radiant , dire = senate .count (Radiant .mark ), senate .count (Dire .mark )
19
+ radiant_bans , dire_bans = 0 , 0
20
+
21
+ last = senate [0 ]
22
+
23
+ while queue and radiant != radiant_banned and dire != dire_banned :
24
+ left = queue .popleft ()
25
+
26
+ if left == Radiant .mark :
27
+ if radiant_banned > 0 :
28
+ radiant_banned -= 1
29
+ radiant_bans += 1
30
+ else :
31
+ dire_banned += 1
32
+ queue .append (left )
33
+ last = left
34
+ else :
35
+ if dire_banned > 0 :
36
+ dire_banned -= 1
37
+ dire_bans += 1
38
+ else :
39
+ radiant_banned += 1
40
+ queue .append (left )
41
+ last = left
42
+
43
+ return Radiant .name if last == Radiant .mark else Dire .name
44
+
45
+ def predictPartyVictory1 (self , senate : str ) -> str :
46
+ radiant_banned , dire_banned = 0 , 0
47
+ radiant_bans , dire_bans = 0 , 0
48
+ radiant , dire = senate .count (Radiant .mark ), senate .count (Dire .mark )
49
+ banned = 0
50
+
51
+ pos = 0
52
+ while radiant != radiant_banned and dire != dire_banned :
53
+ pos = pos % len (senate )
54
+ senator = senate [pos ]
55
+ if banned & 1 << pos :
56
+ pass
57
+ elif senator == Radiant .mark :
58
+ if radiant_bans > 0 :
59
+ radiant_bans -= 1
60
+ radiant_banned += 1
61
+ banned |= 1 << pos
62
+ else :
63
+ dire_bans += 1
64
+ else :
65
+ if dire_bans > 0 :
66
+ dire_bans -= 1
67
+ dire_banned += 1
68
+ banned |= 1 << pos
69
+ else :
70
+ radiant_bans += 1
71
+ pos += 1
72
+
73
+ if dire == dire_banned :
74
+ return Radiant .name
75
+
76
+ return Dire .name
You can’t perform that action at this time.
0 commit comments