1578. Minimum Time to Make Rope Colorful

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 1578

class Solution:
    def minCost(self, colors: str, neededTime: List[int]) -> int:
        res = i = 0
        n = len(colors)
        while i < n:
            j = i
            s = mx = 0
            while j < n and colors[j] == colors[i]:
                s += neededTime[j]
                if mx < neededTime[j]:
                    mx = neededTime[j]
                j += 1
            if j - i > 1:
                res += s - mx
            i = j
        return res