leetcode - 1578. Minimum Time to Make Rope Colorful

Description

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:
在这里插入图片描述

Input: colors = "abaac", neededTime = [1,2,3,4,5]
Output: 3
Explanation: In the above image, 'a' is blue, 'b' is red, and 'c' is green.
Bob can remove the blue balloon at index 2. This takes 3 seconds.
There are no longer two consecutive balloons of the same color. Total time = 3.

Example 2:
在这里插入图片描述

Input: colors = "abc", neededTime = [1,2,3]
Output: 0
Explanation: The rope is already colorful. Bob does not need to remove any balloons from the rope.

Example 3:
在这里插入图片描述

Input: colors = "aabaa", neededTime = [1,2,3,4,1]
Output: 2
Explanation: Bob will remove the ballons at indices 0 and 4. Each ballon takes 1 second to remove.
There are no longer two consecutive balloons of the same color. Total time = 1 + 1 = 2.

Constraints:

n == colors.length == neededTime.length
1 <= n <= 10^5
1 <= neededTime[i] <= 10^4
colors contains only lowercase English letters.

Solution

Heap

Go through the balloons, if the color is different than the previous one, handle the previous one. Because if there are 3 consecutive balloons, we have to remove 2 of them, so use a heap to store all the time, and pop until the length of the heap is only 1.

Time complexity: o ( n log ⁡ n ) o(n\log n) o(nlogn)
Space complexity: o ( n ) o(n) o(n)

Reduced from heap to numbers

Use a sum to store all the time, and the effort should be the sum - maxValue

Time complexity: o ( n ) o(n) o(n)
Space complexity: o ( n ) o(n) o(n)

Code

Heap

class Solution:
    def minCost(self, colors: str, neededTime: List[int]) -> int:
        heap = []
        prev_b = None
        res = 0
        for i in range(len(colors)):
            if colors[i] != prev_b:
                if prev_b:
                    while len(heap) > 1:
                        res += heapq.heappop(heap)
                    heapq.heappop(heap)
                prev_b = colors[i]
            heapq.heappush(heap, neededTime[i])
        while len(heap) > 1:
            res += heapq.heappop(heap)
        return res

Reduced

class Solution:
    def minCost(self, colors: str, neededTime: List[int]) -> int:
        res = 0
        cur_sum = 0
        max_val = 0
        prev_b = None
        for i in range(len(colors)):
            if colors[i] != prev_b:
                if prev_b:
                    res += cur_sum - max_val
                    cur_sum = 0
                    max_val = 0
                prev_b = colors[i]
            cur_sum += neededTime[i]
            max_val = max(max_val, neededTime[i])
        res += cur_sum - max_val
        return res
  • 19
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值