LeetCode Challenge Day 49 β 1578. Minimum Time to Make Rope Colorful
Nitin Ahirwal / November 3, 2025
Hey folks π
This is Day 49 of my LeetCode streak π
Todayβs problem is 1578. Minimum Time to Make Rope Colorful β a medium-level greedy problem involving strings and optimization of removal costs.
π Problem Statement
Alice has n balloons arranged on a rope, represented by the string colors,
where colors[i] is the color of the ith balloon.
Each balloon requires neededTime[i] seconds to remove.
Alice wants the rope to be colorful, meaning no two consecutive balloons have the same color.
Return the minimum total time needed to remove balloons to achieve this.
π‘ Intuition
When two or more consecutive balloons have the same color, we must remove all but one.
To minimize total time, we should keep the balloon that takes the longest to remove and remove the rest.
So, for every group of identical consecutive colors,
we calculate:
β sum of removal times β maximum removal time
and add it to our total cost.
π Approach
- Iterate through the
colorsstring. - For each consecutive group of same-colored balloons:
- Calculate the total sum of removal times (
runSum). - Find the maximum removal time in that group (
runMax). - Add
(runSum - runMax)to the total cost.
- Calculate the total sum of removal times (
- When the color changes, reset the group tracking variables.
- After the loop, add the cost from the last group.
This ensures we only remove the minimum necessary balloons to make the rope colorful.
β±οΈ Complexity Analysis
-
Time complexity:
( O(n) ) β Single traversal through the balloons. -
Space complexity:
( O(1) ) β Only a few variables are used for sums and maximums.
π§βπ» Code (JavaScript)
/**
* @param {string} colors
* @param {number[]} neededTime
* @return {number}
*/
var minCost = function(colors, neededTime) {
const n = colors.length;
if (n === 0) return 0;
let total = 0;
let runSum = neededTime[0];
let runMax = neededTime[0];
for (let i = 1; i < n; i++) {
if (colors[i] === colors[i - 1]) {
runSum += neededTime[i];
runMax = Math.max(runMax, neededTime[i]);
} else {
total += runSum - runMax; // remove all but the max in the previous run
runSum = neededTime[i];
runMax = neededTime[i];
}
}
// finalize the last run
total += runSum - runMax;
return total;
};
// Tests (from the prompt)
console.log(minCost("abaac", [1,2,3,4,5])); // 3
console.log(minCost("abc", [1,2,3])); // 0
console.log(minCost("aabaa", [1,2,3,4,1])); // 2
π§ͺ Example Walkthrough
Input: colors = "abaac", neededTime = [1,2,3,4,5]
Steps:
Balloons a(1) and a(3) β remove the one with less time (3 seconds). β Total = 3
Output: 3
π― Reflection
This problem beautifully showcases how greedy choices can minimize cost efficiently. By always keeping the balloon with the highest removal time, we ensure no unnecessary deletions and achieve the optimal result.
Thatβs it for Day 49 of my LeetCode journey πͺ See you tomorrow for Day 50 β letβs keep the streak alive and consistent! π₯
Happy Coding π¨βπ»