|
| 1 | +# 1578. Minimum Time to Make Rope Colorful |
| 2 | +# 🟠 Medium |
| 3 | +# |
| 4 | +# https://leetcode.com/problems/minimum-time-to-make-rope-colorful/ |
| 5 | +# |
| 6 | +# Tags: Array - String - Dynamic Programming - Greedy |
| 7 | + |
| 8 | +import timeit |
| 9 | +from typing import List |
| 10 | + |
| 11 | + |
| 12 | +# We can see that to solve the problem we need to find all sequences of |
| 13 | +# the same color and, for each, eliminate all balloons except for one, |
| 14 | +# we can use a greedy approach because we know that the most efficient |
| 15 | +# solution will be to keep the balloon with the highest removal cost and |
| 16 | +# keep the others. We iterate over the balloons keeping the color of the |
| 17 | +# current sequence and the highest removal cost that we have seen in the |
| 18 | +# current sequence in two variables, if the balloon we visit has the |
| 19 | +# same color of the current sequence, we know that we need to remove |
| 20 | +# either this balloon or the previous one we decided to keep, we |
| 21 | +# choose to remove the one with the smaller cost and add that cost to |
| 22 | +# the result. When we get to a balloon with a different color, we |
| 23 | +# temporarily decide to keep it and store that color as the current |
| 24 | +# sequence color and its cost as the highest cost. |
| 25 | +# |
| 26 | +# Time complexity: O(n) - We visit each element once and perform O(1) |
| 27 | +# tasks. |
| 28 | +# Space complexity: O(1) - We only store three variables. Constant space. |
| 29 | +# |
| 30 | +# Runtime: 1040 ms, faster than 98.87% |
| 31 | +# Memory Usage: 25 MB, less than 52.92% |
| 32 | +class Solution: |
| 33 | + def minCost(self, colors: str, neededTime: List[int]) -> int: |
| 34 | + # Store the color of the current sequence. |
| 35 | + current_color = None |
| 36 | + # Store the highest value seen for the current sequence. |
| 37 | + current_highest = 0 |
| 38 | + # Initialize the result. |
| 39 | + res = 0 |
| 40 | + # Iterate over the balloons. |
| 41 | + for i in range(len(colors)): |
| 42 | + # If this is a new color, reinitialize the sequence. |
| 43 | + if current_color != colors[i]: |
| 44 | + current_highest = neededTime[i] |
| 45 | + current_color = colors[i] |
| 46 | + # Else, if we are inside a sequence. |
| 47 | + else: |
| 48 | + # Check which balloon in the sequence had a higher cost |
| 49 | + # and add the other to the time cost. |
| 50 | + if neededTime[i] > current_highest: |
| 51 | + # If the current cost is higher, evict the previous |
| 52 | + # highest cost and add it to the result. |
| 53 | + res += current_highest |
| 54 | + current_highest = neededTime[i] |
| 55 | + else: |
| 56 | + # Otherwise, if they are equal or the current is |
| 57 | + # lower, add that one to the cost. |
| 58 | + res += neededTime[i] |
| 59 | + return res |
| 60 | + |
| 61 | + |
| 62 | +def test(): |
| 63 | + executors = [Solution] |
| 64 | + tests = [ |
| 65 | + ["abc", [1, 2, 3], 0], |
| 66 | + ["aabaa", [1, 2, 3, 4, 1], 2], |
| 67 | + ["abaac", [1, 2, 3, 4, 5], 3], |
| 68 | + ["bbbaaa", [4, 9, 3, 8, 8, 9], 23], |
| 69 | + ] |
| 70 | + for executor in executors: |
| 71 | + start = timeit.default_timer() |
| 72 | + for _ in range(1): |
| 73 | + for col, t in enumerate(tests): |
| 74 | + sol = executor() |
| 75 | + result = sol.minCost(t[0], t[1]) |
| 76 | + exp = t[2] |
| 77 | + assert result == exp, ( |
| 78 | + f"\033[93m» {result} <> {exp}\033[91m for" |
| 79 | + + f" test {col} using \033[1m{executor.__name__}" |
| 80 | + ) |
| 81 | + stop = timeit.default_timer() |
| 82 | + used = str(round(stop - start, 5)) |
| 83 | + cols = "{0:20}{1:10}{2:10}" |
| 84 | + res = cols.format(executor.__name__, used, "seconds") |
| 85 | + print(f"\033[92m» {res}\033[0m") |
| 86 | + |
| 87 | + |
| 88 | +test() |
0 commit comments