Minimum Time to Make Rope Colorful

MediumGreedygreedy

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Balloons hang in a row on a rope. colors[i] is the color (a lowercase letter) of balloon i, and removing balloon i takes neededTime[i] seconds. The rope is colorful when no two consecutive balloons have the same color. You may remove any balloons you like; equivalently, every maximal run of consecutive equal colors must be reduced to exactly one balloon. Return the minimum total time needed to make the rope colorful.

Examples

Input: colors = "abaac", neededTime = [1,2,3,4,5]
Output: 3
Balloons 2 and 3 are both 'a'; remove balloon 2 (3 seconds) and keep balloon 3.
Input: colors = "abc", neededTime = [1,2,3]
Output: 0
No two neighbors share a color.
Input: colors = "aabaa", neededTime = [1,2,3,4,1]
Output: 2
Remove balloon 0 (1 second) and balloon 4 (1 second).

Constraints

  • n == colors.length == neededTime.length
  • 1 <= n <= 10^5
  • 1 <= neededTime[i] <= 10^4
  • colors consists of lowercase English letters

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More greedy problems