从原始的string中,找出使用单个字母重复出现组成的substring,找到其起点i和终点j-1.由于不能重复,所以重复字母的字串对应的时间除了最大时间外对应的气球,其他的都要去除。所以每当找到重复子串,加上这段内的气球对应的全部移除时间,把最大时间对应的气球保留,于是在刚才的时间内-这段内的最大时间。
class Solution:
def minCost(self, colors: str, neededTime: List[int]) -> int:
i = 0
res = 0
colors = list(colors)
while i < len(colors):
j = i + 1
if j < len(colors) and colors[j] == colors[i]:
while j < len(colors) and colors[j] == colors[i]:
j += 1
for idx in range(i, j):
res += neededTime[idx]
res -= max(neededTime[i:j])
i = j
else:
i += 1
return res