Uriel's Corner

Research Associate @ Harvard University / Research Interests: Computer Vision, Biomedical Image Analysis, Machine Learning
posts - 0, comments - 50, trackbacks - 0, articles - 594
给定一列字符串R和D,代表两个阵营,每次轮到某个人的时候,可以选择ban下一个人,或者如果当前所有人都属于同一阵营时,宣告该阵营胜利,输出最后胜利的阵营,模拟题


 1 #649
 2 #Runtime: 47 ms (Beats 70%)
 3 #Memory: 13.9 MB (Beats 15%)
 4 
 5 class Solution(object):
 6     def predictPartyVictory(self, senate):
 7         """
 8         :type senate: str
 9         :rtype: str
10         """
11         n = len(senate)
12         s, fg = set(), [False] * n
13         b_d = b_r = 0
14         while len(s) != 1:
15             s = set()
16             for i in range(n):
17                 if not fg[i]:
18                     if senate[i] == 'R':
19                         if b_r > 0: 
20                             b_r -= 1
21                             fg[i] = True
22                         else:
23                             b_d += 1
24                             s.add('R')
25                     else:        
26                         if b_d > 0:
27                             b_d -= 1
28                             fg[i] = True
29                         else:
30                             b_r += 1
31                             s.add('D')
32         return 'Radiant' if 'R' in s else 'Dire'

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理