有n个站点需要收垃圾,有MPG三种垃圾,每种垃圾每出现一次需要1个单位时间回收,从i到i+1车辆运行时间travel[i],问回收完所有垃圾需要多久,模拟
1 #2391
2 #Runtime: 900 ms (Beats 27%)
3 #Memory: 42.5 MB (Beats 81%)
4
5 class Solution(object):
6 def garbageCollection(self, garbage, travel):
7 """
8 :type garbage: List[str]
9 :type travel: List[int]
10 :rtype: int
11 """
12 ans = 0
13 last = [-1, -1, -1]
14 cnt = [0, 0, 0]
15 for i, st in enumerate(garbage):
16 for ch in st:
17 if ch == 'M':
18 last[0] = i
19 if ch == 'P':
20 last[1] = i
21 if ch == 'G':
22 last[2] = i
23 ans += len(st)
24 for i in xrange(len(travel)):
25 if last[0] >= i + 1:
26 ans += travel[i]
27 if last[1] >= i + 1:
28 ans += travel[i]
29 if last[2] >= i + 1:
30 ans += travel[i]
31 return ans