|
| 1 | +# 1578. Minimum Time to Make Rope Colorful |
| 2 | + |
| 3 | +Alice has `n` balloons arranged on a rope. |
| 4 | +You are given a 0-indexed string `colors` where `colors[i]` is the color of the $i^{th}$ balloon. |
| 5 | + |
| 6 | +Alice wants the rope to be colorful. |
| 7 | +She does not want two consecutive balloons to be of the same color, so she asks Bob for help. |
| 8 | +Bob can remove some balloons from the rope to make it colorful. |
| 9 | +You are given a 0-indexed integer array `neededTime` where `neededTime[i]` is the time (in seconds) that Bob needs to remove the $i^{th}$ balloon from the rope. |
| 10 | + |
| 11 | +Return the minimum time Bob needs to make the rope colorful. |
| 12 | + |
| 13 | +**Constraints:** |
| 14 | + |
| 15 | +- `n == colors.length == neededTime.length` |
| 16 | +- `1 <= n <= 10^5` |
| 17 | +- `1 <= neededTime[i] <= 10^4` |
| 18 | +- `colors` contains only lowercase English letters. |
| 19 | + |
| 20 | +## 基礎思路 |
| 21 | + |
| 22 | +本題要求使繩子上的氣球顏色「相鄰不重複」,也就是要移除最少時間成本的氣球,使最終沒有兩顆相鄰氣球顏色相同。給定: |
| 23 | + |
| 24 | +- 字串 `colors` 表示氣球顏色; |
| 25 | +- 陣列 `neededTime[i]` 表示移除第 `i` 顆氣球所需的時間。 |
| 26 | + |
| 27 | +若出現連續相同顏色的氣球,至少要移除其中的幾顆,使該段區間僅留下**一顆顏色相同的氣球**。目標是最小化所有移除的時間總和。 |
| 28 | + |
| 29 | +在思考解法時,我們需注意: |
| 30 | + |
| 31 | +- **相同顏色連續群組的處理**:對每一段相同顏色的連續區間,必須移除除最大時間氣球外的其餘氣球; |
| 32 | +- **單次遍歷**:每個氣球僅需一次掃描,累積當前連續區間資訊即可; |
| 33 | +- **最佳性原則**:在每段連續相同顏色中,保留「移除時間最大的氣球」,刪除其他者。 |
| 34 | + |
| 35 | +因此整體策略為: |
| 36 | + |
| 37 | +- **逐一遍歷氣球**,將連續相同顏色視為一個「群組」; |
| 38 | +- 對每個群組計算: |
| 39 | + |
| 40 | + - 群組內所有氣球移除時間總和; |
| 41 | + - 群組內最大移除時間; |
| 42 | + - 該群組最小移除成本為 `(sum - max)`; |
| 43 | +- 將所有群組的最小移除成本加總,即為答案。 |
| 44 | + |
| 45 | +此方法僅需一次遍歷,時間與空間皆為線性。 |
| 46 | + |
| 47 | +## 解題步驟 |
| 48 | + |
| 49 | +### Step 1:處理小規模輸入 |
| 50 | + |
| 51 | +若只有一顆氣球或更少,無需移除,直接回傳 `0`。 |
| 52 | + |
| 53 | +```typescript |
| 54 | +// 若只有一顆氣球,無需移除 |
| 55 | +if (colors.length <= 1) { |
| 56 | + return 0; |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +### Step 2:初始化狀態變數 |
| 61 | + |
| 62 | +紀錄當前顏色群組的顏色代碼、累積時間總和、群組內最大時間,並初始化總移除時間。 |
| 63 | + |
| 64 | +```typescript |
| 65 | +// 以首顆氣球為初始群組狀態 |
| 66 | +let previousColorCode = colors.charCodeAt(0); |
| 67 | +let currentGroupSum = neededTime[0]; |
| 68 | +let currentGroupMax = neededTime[0]; |
| 69 | + |
| 70 | +// 總移除時間累加器 |
| 71 | +let totalRemovalTime = 0; |
| 72 | +``` |
| 73 | + |
| 74 | +### Step 3:遍歷氣球,檢查是否連續相同顏色 |
| 75 | + |
| 76 | +對每顆氣球進行一次迴圈: |
| 77 | + |
| 78 | +* 若顏色相同,累加群組總時間並更新最大值; |
| 79 | +* 若顏色不同,代表前一群組結束,計算群組貢獻並重設群組狀態。 |
| 80 | + |
| 81 | +```typescript |
| 82 | +// 逐一遍歷每顆氣球,處理相同顏色群組 |
| 83 | +for (let index = 1; index < colors.length; index++) { |
| 84 | + const currentColorCode = colors.charCodeAt(index); |
| 85 | + const currentTime = neededTime[index]; |
| 86 | + |
| 87 | + if (currentColorCode === previousColorCode) { |
| 88 | + // 同顏色群組:累加時間並更新最大值 |
| 89 | + currentGroupSum += currentTime; |
| 90 | + if (currentTime > currentGroupMax) { |
| 91 | + currentGroupMax = currentTime; |
| 92 | + } |
| 93 | + } else { |
| 94 | + // 顏色改變:群組結束,加入最小移除成本 (sum - max) |
| 95 | + totalRemovalTime += (currentGroupSum - currentGroupMax); |
| 96 | + |
| 97 | + // 開始新群組 |
| 98 | + previousColorCode = currentColorCode; |
| 99 | + currentGroupSum = currentTime; |
| 100 | + currentGroupMax = currentTime; |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +### Step 4:處理最後一個群組 |
| 106 | + |
| 107 | +由於最後一段不會在迴圈中自動結算,需在結尾手動加上。 |
| 108 | + |
| 109 | +```typescript |
| 110 | +// 處理最後一段相同顏色群組 |
| 111 | +totalRemovalTime += (currentGroupSum - currentGroupMax); |
| 112 | +``` |
| 113 | + |
| 114 | +### Step 5:回傳最小總移除時間 |
| 115 | + |
| 116 | +```typescript |
| 117 | +// 回傳最終總移除時間 |
| 118 | +return totalRemovalTime; |
| 119 | +``` |
| 120 | + |
| 121 | +## 時間複雜度 |
| 122 | + |
| 123 | +- 單次遍歷整個字串長度 `n`; |
| 124 | +- 每次操作僅為常數時間更新(加總、比較、重設群組)。 |
| 125 | +- 總時間複雜度為 $O(n)$。 |
| 126 | + |
| 127 | +> $O(n)$ |
| 128 | +
|
| 129 | +## 空間複雜度 |
| 130 | + |
| 131 | +- 僅使用固定變數追蹤狀態(顏色、加總、最大值); |
| 132 | +- 不需額外資料結構。 |
| 133 | +- 總空間複雜度為 $O(1)$。 |
| 134 | + |
| 135 | +> $O(1)$ |
0 commit comments