-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1578-minimum-time-to-make-rope-colorful.js
46 lines (41 loc) · 1.28 KB
/
1578-minimum-time-to-make-rope-colorful.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* 1578. Minimum Time to Make Rope Colorful
* https://leetcode.com/problems/minimum-time-to-make-rope-colorful/
* Difficulty: Medium
*
* 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. She does not want two consecutive balloons to be of the same
* color, so she asks Bob for help. Bob can remove some balloons from the rope to make it colorful.
* You are given a 0-indexed integer array neededTime where neededTime[i] is the time (in seconds)
* that Bob needs to remove the ith balloon from the rope.
*
* Return the minimum time Bob needs to make the rope colorful.
*/
/**
* @param {string} colors
* @param {number[]} neededTime
* @return {number}
*/
var minCost = function(colors, neededTime) {
let result = 0;
let i = 0;
while (i < colors.length - 1) {
if (colors[i] === colors[i + 1]) {
let maxTime = neededTime[i];
let sumTime = neededTime[i];
let j = i + 1;
while (j < colors.length && colors[j] === colors[i]) {
maxTime = Math.max(maxTime, neededTime[j]);
sumTime += neededTime[j];
j++;
}
result += sumTime - maxTime;
i = j;
} else {
i++;
}
}
return result;
};