题目浅析

思路分享

  • 贪心策略,先计算代价之和,然后去除每一段颜色中,代价最大的气球即可。

代码解答(强烈建议自行解答后再看)

  • 参考题解
1
2
3
4
5
6
7
8
9
10
11
12
class Solution:
def minCost(self, colors: str, neededTime: List[int]) -> int:
ans = cur_max = 0
n = len(colors)
for i, x in enumerate(neededTime):
ans += x
if cur_max < x:
cur_max = x
if i == n-1 or colors[i] != colors[i+1]:
ans -= cur_max
cur_max = 0
return ans