Minimum Time to Make Rope Colorful LeetCode solution in Python

1
# Runtime: 862 ms (Top 94.6%) | Memory: 27.26 MB (Top 71.4%)
2

3

4
class Solution:
5
def minCost(self, s: str, cost: List[int]) -> int:
6
ans = prev = 0 # index of previously retained letter
7
for i in range(1, len(s)):
8
if s[prev] != s[i]:
9
prev = i
10
else:
11
ans += min(cost[prev], cost[i])
12
if cost[prev] < cost[i]:
13
prev = i
14
return ans

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0