Leetcode 1578. Minimum Time to Make Rope Colorful [Python]

从原始的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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值