Skip to content

Commit 70db45c

Browse files
committed
Add solution #1578
1 parent e2ce16f commit 70db45c

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,380 LeetCode solutions in JavaScript
1+
# 1,381 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1205,6 +1205,7 @@
12051205
1575|[Count All Possible Routes](./solutions/1575-count-all-possible-routes.js)|Hard|
12061206
1576|[Replace All ?'s to Avoid Consecutive Repeating Characters](./solutions/1576-replace-all-s-to-avoid-consecutive-repeating-characters.js)|Medium|
12071207
1577|[Number of Ways Where Square of Number Is Equal to Product of Two Numbers](./solutions/1577-number-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers.js)|Medium|
1208+
1578|[Minimum Time to Make Rope Colorful](./solutions/1578-minimum-time-to-make-rope-colorful.js)|Medium|
12081209
1598|[Crawler Log Folder](./solutions/1598-crawler-log-folder.js)|Easy|
12091210
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12101211
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* 1578. Minimum Time to Make Rope Colorful
3+
* https://leetcode.com/problems/minimum-time-to-make-rope-colorful/
4+
* Difficulty: Medium
5+
*
6+
* Alice has n balloons arranged on a rope. You are given a 0-indexed string colors where colors[i]
7+
* is the color of the ith balloon.
8+
*
9+
* Alice wants the rope to be colorful. She does not want two consecutive balloons to be of the same
10+
* color, so she asks Bob for help. Bob can remove some balloons from the rope to make it colorful.
11+
* You are given a 0-indexed integer array neededTime where neededTime[i] is the time (in seconds)
12+
* that Bob needs to remove the ith balloon from the rope.
13+
*
14+
* Return the minimum time Bob needs to make the rope colorful.
15+
*/
16+
17+
/**
18+
* @param {string} colors
19+
* @param {number[]} neededTime
20+
* @return {number}
21+
*/
22+
var minCost = function(colors, neededTime) {
23+
let result = 0;
24+
let i = 0;
25+
26+
while (i < colors.length - 1) {
27+
if (colors[i] === colors[i + 1]) {
28+
let maxTime = neededTime[i];
29+
let sumTime = neededTime[i];
30+
let j = i + 1;
31+
32+
while (j < colors.length && colors[j] === colors[i]) {
33+
maxTime = Math.max(maxTime, neededTime[j]);
34+
sumTime += neededTime[j];
35+
j++;
36+
}
37+
38+
result += sumTime - maxTime;
39+
i = j;
40+
} else {
41+
i++;
42+
}
43+
}
44+
45+
return result;
46+
};

0 commit comments

Comments
 (0)