Skip to content

Commit 3b13bda

Browse files
committed
LeetCode 1578. Minimum Time to Make Rope Colorful
1 parent 2160bed commit 3b13bda

File tree

3 files changed

+99
-3
lines changed

3 files changed

+99
-3
lines changed

README.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,10 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
242242
| [1461. Check If a String Contains All Binary Codes of Size K][lc1461] | 🟠 Medium | [![python](res/py.png)][lc1461py] |
243243
| [1465. Maximum Area of a Piece of Cake After Cuts][lc1465] | 🟠 Medium | [![python](res/py.png)][lc1465py] |
244244
| [1473. Paint House III][lc1473] | 🔴 Hard | [![python](res/py.png)][lc1473py] |
245-
| [1480. Running Sum of 1d Array][lc1480] | 🟢 Easy | [![python](res/py.png)](leetcode/running_sum.py) |
246-
| [1603. Design Parking System][lc1603] | 🟢 Easy | [![python](res/py.png)](leetcode/design-parking-system.py) |
247-
| [1642. Furthest Building You Can Reach][lc1642] | 🟠 Medium | [![python](res/py.png)](leetcode/furthest-building-you-can-reach.py) |
245+
| [1480. Running Sum of 1d Array][lc1480] | 🟢 Easy | [![python](res/py.png)][lc1480py] |
246+
| [1578. Minimum Time to Make Rope Colorful][lc1578] | 🟠 Medium | [![python](res/py.png)][lc1578py] |
247+
| [1603. Design Parking System][lc1603] | 🟢 Easy | [![python](res/py.png)][lc1603py] |
248+
| [1642. Furthest Building You Can Reach][lc1642] | 🟠 Medium | [![python](res/py.png)][lc1642py] |
248249
| [1647. Minimum Deletions to Make Character Frequencies Unique][lc1647] | 🟠 Medium | [![python](res/py.png)][lc1647py] |
249250
| [1658. Minimum Operations to Reduce X to Zero][lc1658] | 🟠 Medium | [![python](res/py.png)][lc1658py] |
250251
| [1680. Concatenation of Consecutive Binary Numbers][lc1680] | 🟠 Medium | [![python](res/py.png)][lc1680py] |
@@ -709,8 +710,13 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
709710
[lc1473]: https://leetcode.com/problems/paint-house-iii/
710711
[lc1473py]: leetcode/paint-house-iii.py
711712
[lc1480]: https://leetcode.com/problems/running-sum-of-1d-array/
713+
[lc1480py]: leetcode/running_sum.py
714+
[lc1578]: https://leetcode.com/problems/minimum-time-to-make-rope-colorful/
715+
[lc1578py]: leetcode/minimum-time-to-make-rope-colorful.py
712716
[lc1603]: https://leetcode.com/problems/design-parking-system/
717+
[lc1603py]: leetcode/design-parking-system.py
713718
[lc1642]: https://leetcode.com/problems/furthest-building-you-can-reach/
719+
[lc1642py]: leetcode/furthest-building-you-can-reach.py
714720
[lc1647]: https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/
715721
[lc1647py]: leetcode/minimum-deletions-to-make-character-frequencies-unique.py
716722
[lc1658]: https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/

leetcode/lists/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Ignore temporary files
2+
tmp*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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

Comments
 (0)