Minimum Time to Make Rope Colorful

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: no two consecutive balloons may have the same color. Bob can remove balloons, and removing the ith balloon takes neededTime[i] seconds. Return the minimum time Bob needs to make the rope colorful.

30:00

Minimum Time to Make Rope Colorful
medium
Free written solution
Topics
Companies

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: no two consecutive balloons may have the same color. Bob can remove balloons, and removing the ith balloon takes neededTime[i] seconds. Return the minimum time Bob needs to make the rope colorful.

Example 1:
Input: {"colors":"abaac","neededTime":[1,2,3,4,5]}
Output: 3
Constraints:
  • n==colors.length==neededTime.lengthn == \text{colors.length} == \text{neededTime.length}

  • 1≤n≤1051 \leq n \leq 10^5

  • 1≤neededTime[i]≤1041 \leq \text{neededTime}[i] \leq 10^4

  • colors\text{colors} contains only lowercase English letters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"colors":"abaac","neededTime":[1,2,3,4,5]}

total = 0: no balloon has been popped yet. We will walk the rope of 5 balloons once.

a

1

b

2

a

3

a

4

c

5

kept in this run

-

time spent popping

0

Variables
VariableValue
i-
keep-
total0
DepthFunction Call
Stack empty
0/6