-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm1717 Daily v2.py
49 lines (41 loc) · 1.44 KB
/
m1717 Daily v2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class Solution:
def maximumGain(self, s: str, x: int, y: int) -> int:
# Non-(a,b) chars act as boundaries and don't affect the final score
chunks = re.findall('[a-b]+', s)
# Store largest in x
t1, t2 = 'ab', 'ba'
if y > x :
x, y = y, x
t1, t2 = t2, t1
# Parse each chunk
output = 0
for chk in chunks :
rev = False
findX = True
findY = True
stk = list(chk)
# findX: check for the larger value ab/ba
# findY: check for the smaller
while findX or findY :
stk2 = []
target = t1 if findX else t2
targetRew = x if findX else y
while stk :
if not stk2 :
stk2.append(stk.pop())
continue
curr = stk[-1] + stk2[-1] if not rev else stk2[-1] + stk[-1]
if curr == target :
output += targetRew
stk.pop()
stk2.pop()
continue
stk2.append(stk.pop())
if not findX :
break
findX = False
# reverse ab ba since stack appending to another stack
# reverses the order
rev = not rev
stk = stk2
return output