Leetcode 1700 Solution

This article provides solution to leetcode question 1700 (minimum-time-to-make-rope-colorful)

https://leetcode.com/problems/minimum-time-to-make-rope-colorful

Solution

class Solution:
    def minCost(self, colors: str, neededTime: List[int]) -> int:
        i = 0
        ans = 0
        while i < len(colors):
            start = i
            end = i

            while end < len(colors) - 1 and colors[end] == colors[end + 1]:
                end += 1

            if end != start:
                ans += sum(neededTime[start:end + 1]) - max(neededTime[start:end + 1])

            i = end + 1

        return ans