NeetCode #209LC-1578MediumTwo Pointers
← Back to All Problems#209 · #1578 · Minimum Time to Make Rope Colorful(使绳子变成彩色的最短时间)
📌 Problem Statement & Constraints
You are given a string
colors and an array neededTime where neededTime[i] is the time to remove the i-th balloon. Two adjacent balloons with the same colour must not both remain. Return the minimum total removal time. Constraints: 1 <= colors.length <= 10^5, colors is lowercase letters, 1 <= neededTime[i] <= 10^4.💡 Core Algorithmic Approaches
- Group the balloons into maximal runs of the same colour.
- Within a run of length
L, exactly one balloon may remain, soL - 1must be removed. - To minimise the cost, keep the balloon with the largest removal time and remove all the others, so the cost is
sum(run) - max(run). - Summing over all runs gives the answer. This is a local greedy that is globally optimal because runs are independent.
💻 Benchmark Python3 Implementation
class Solution:
def minCost(self, colors: str, neededTime: List[int]) -> int:
res = 0
i = 0
n = len(colors)
while i < n:
j = i
total = 0
mx = 0
while j < n and colors[j] == colors[i]:
total += neededTime[j]
mx = max(mx, neededTime[j])
j += 1
res += total - mx # keep the most expensive balloon
i = j
return res⚡ Complexity Deep Dive
⏱️ Time Complexity
O(n): each balloon is visited once.
💾 Space Complexity
O(1): three counters per run.
⚠️ Interview Pitfalls & Follow-ups
- Keeping the cheapest balloon in each run: the goal is to minimise the removal cost, so the most expensive balloon should remain.
- Removing all balloons in a run: at most one must be removed only when the run length exceeds 1, and exactly one may remain.
- Assuming the whole string is one run: the grouping by colour is essential.
- Using
sum(run) - max(run)for runs of length 1: that gives 0, which is correct -- a lone balloon needs no removal.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🧩 Related Two Pointers Problems
中文速览与面试要点
使绳子变成彩色的最短时间
Alice 把 n 个气球绑在绳子上,每个气球有一个颜色 colors[i] 和移除它所需的时间 neededTime[i]。Alice 想要移除一些气球,使得绳子中 没有两个连续的气球颜色相同 。请你返回移除气球所需的最少总时间。约束:n == colors.length == neededTime.length;1 ≤ n ≤ 10^5;1 ≤ neededTime[i] ≤ 10^4;colors 只包含小写英文字母。
核心要点
- 按颜色分段贪心(标准解) :把绳子按「连续同色」切分成若干段。对每段,必须移除到只剩一个气球,因此应保留 移除时间最大 的那个,移除其余全部。
- 段的代价 : 该段 neededTime 之和 − 该段 neededTime 的最大值 。
- 为什么贪心最优 :每段最终只能留一个气球,保留哪个都满足「无相邻同色」的约束(因为段之间颜色不同),因此保留代价最大的那个最省时间。
- 复杂度 :单次遍历,O(n)。
常见陷阱
- 对每段只移除一个气球 :若段长为 L,必须移除 L−1 个(只留一个),而不是移除 1 个。
- 保留移除时间最小的 :应保留 最大 的,这样移除代价最小。
- 用「相邻不同色就累加」的简单逻辑 :那会漏掉长度为 3 及以上的同色段(如 "aaa" 需要移除 2 个)。
- 误以为需要动态规划 :由于段之间相互独立且约束只作用于段内,贪心即可。
参考实现
class Solution:
def minCost(self, colors: str, neededTime: list[int]) -> int:
total = 0
n = len(colors)
i = 0
while i < n:
j = i
seg_sum = 0
seg_max = 0
# 扫描当前同色连续段
while j < n and colors[j] == colors[i]:
seg_sum += neededTime[j]
if neededTime[j] > seg_max:
seg_max = neededTime[j]
j += 1
total += seg_sum - seg_max # 移除除最大者之外的全部
i = j
return total