#1578

Minimum Time to Make Rope Colorful

specialist · 630 · lc medium +30 · verified · 65.2% accepted · 4,335 likes · top 69%

Description

A rope has n balloons with colors given by colors[i]. Bob needs to remove balloons so no two adjacent remaining balloons share the same color. The cost to remove balloon i is neededTime[i]. Return the minimum total time for Bob to make the rope colorful.

Example 1:

Input: colors = "abaac", neededTime = [1,2,3,4,5]
Output: 3
Explanation: In the above image, 'a' is blue, 'b' is red, and 'c' is green.
Bob can remove the blue balloon at index 2. This takes 3 seconds.
There are no longer two consecutive balloons of the same color. Total time = 3.

Example 2:

Input: colors = "abc", neededTime = [1,2,3]
Output: 0
Explanation: The rope is already colorful. Bob does not need to remove any balloons from the rope.

Example 3:

Input: colors = "aabaa", neededTime = [1,2,3,4,1]
Output: 2
Explanation: Bob will remove the balloons at indices 0 and 4. Each balloons takes 1 second to remove.
There are no longer two consecutive balloons of the same color. Total time = 1 + 1 = 2.

Code

1
2
3