1578. Minimum Time to Make Rope Colorful
Medium
Last updated
Medium
Last updated
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.
Example 1:
Example 2:
Example 3:
Constraints:
n == colors.length == neededTime.length
1 <= n <= 105
1 <= neededTime[i] <= 104
colors
contains only lowercase English letters.
這題我的解法是不要用「刪除」來想,而是一個一個挑氣球,先將第一個氣球放進 stack ,若是第二個氣球顏色和第一個相同、但是刪除需要的時間比較多,此時把第一個氣球拿出來,答案加上刪除第一個氣球需要的時間,並把第二個氣球放進去。
如果第二個氣球顏色和前一個氣球不同,此時直接放入氣球即可。
Runtime: 139 ms, faster than 90.30%
Memory Usage: 8.3 MB, less than 100.00%