# 1578. Minimum Time to Make Rope Colorful

#### Medium

***

Alice has `n` balloons arranged on a rope. You are given a **0-indexed** string `colors` where `colors[i]` is the color of the `ith` balloon.

Alice wants the rope to be **colorful**. She does not want **two consecutive balloons** to be of the same color, so she asks Bob for help. Bob can remove some balloons from the rope to make it **colorful**. You are given a **0-indexed** integer array `neededTime` where `neededTime[i]` is the time (in seconds) that Bob needs to remove the `ith` balloon from the rope.

Return *the **minimum time** Bob needs to make the rope **colorful***.

&#x20;

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/12/13/ballon1.jpg)

<pre><code>Input: colors = "abaac", neededTime = [1,2,3,4,5]
<strong>Output:
</strong> 3
<strong>Explanation:
</strong> 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.
</code></pre>

**Example 2:**

![](https://assets.leetcode.com/uploads/2021/12/13/balloon2.jpg)

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

**Example 3:**

![](https://assets.leetcode.com/uploads/2021/12/13/balloon3.jpg)

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

&#x20;

**Constraints:**

* `n == colors.length == neededTime.length`
* `1 <= n <= 105`
* `1 <= neededTime[i] <= 104`
* `colors` contains only lowercase English letters.

```python
class Solution:
    def minCost(self, colors: str, neededTime: List[int]) -> int:
        prev = None
        temp = []
        minTime = 0
        for i in range(len(colors)):
            this = colors[i]
            if prev is None or prev == this:
                temp.append(neededTime[i])
            elif prev != this:
                temp.sort()
                if len(temp) > 1:
                    minTime += sum(temp[:-1])
                temp = []
                temp.append(neededTime[i])
            prev = this
        if len(temp) > 1:
            temp.sort()
            minTime += sum(temp[:-1])
        return minTime
                
```
