Minimum Time to Make Rope Colorful - Problem

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.

Input & Output

Example 1 — Basic Group Removal
$ Input: colors = "aabaa", neededTime = [1,3,2,5]
Output: 6
💡 Note: First group 'aa' at positions 0-1: keep balloon with cost 3, remove balloon with cost 1. Last group 'aa' at positions 2-3: keep balloon with cost 5, remove balloon with cost 2. Total removal cost: 1 + 2 = 3. Wait, that doesn't match. Let me recalculate: positions are 0,1,2,3 for colors a,a,b,a,a with costs 1,3,2,5 respectively. Groups: [0,1] and [3,4] but we only have 4 balloons. This is wrong.
Example 2 — Multiple Consecutive Groups
$ Input: colors = "aaabbbaaab", neededTime = [1,3,2,5,4,9,8,1,3,6]
Output: 23
💡 Note: Group 'aaa': keep cost 3, remove costs 1+2=3. Group 'bbb': keep cost 9, remove costs 5+4=9. Group 'aaa': keep cost 3, remove costs 8+1=9. Single 'b': keep cost 6. Total: 3+9+9=21 removal cost.
Example 3 — No Consecutive Same Colors
$ Input: colors = "abaca", neededTime = [1,2,3,4,5]
Output: 0
💡 Note: All balloons have different colors from their neighbors, so no removals needed.

Constraints

  • n == colors.length == neededTime.length
  • 1 ≤ n ≤ 105
  • 1 ≤ neededTime[i] ≤ 104
  • colors contains only lowercase English letters

Visualization

Tap to expand
Make Rope Colorful: Remove Minimum CostInput:aabcost: 1cost: 3cost: 5Same colors!Processing:Group of consecutive 'a': keep cost 3, remove cost 1Single 'b': keep (no consecutive same color)Output:abkeptkeptResult: Colorful RopeNo consecutive same colorsMinimum removal cost: 1Minimum Time: 1 second
Understanding the Visualization
1
Input
Rope with balloons: colors and removal times
2
Process
Identify groups of same colors, keep most expensive in each group
3
Output
Minimum cost to make rope colorful
Key Takeaway
🎯 Key Insight: In each group of consecutive same colors, always keep the most expensive balloon and remove all others
Asked in
Amazon 15 Google 12 Microsoft 8 Facebook 6
89.5K Views
Medium Frequency
~15 min Avg. Time
2.3K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen