1578. Minimum Time to Make Rope Colorful Medium

@problem@discussion
#Array#String#Dynamic Programming#Greedy



1/**
2 * [1578] Minimum Time to Make Rope Colorful
3 *
4 * Alice has n balloons arranged on a rope. You are given a 0-indexed string colors where colors[i] is the color of the i^th balloon.
5 * 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 i^th balloon from the rope.
6 * Return the minimum time Bob needs to make the rope colorful.
7 *  
8 * Example 1:
9 * <img alt="" src="https://assets.leetcode.com/uploads/2021/12/13/ballon1.jpg" style="width: 404px; height: 243px;" />
10 * Input: colors = "abaac", neededTime = [1,2,3,4,5]
11 * Output: 3
12 * Explanation: In the above image, 'a' is blue, 'b' is red, and 'c' is green.
13 * Bob can remove the blue balloon at index 2. This takes 3 seconds.
14 * There are no longer two consecutive balloons of the same color. Total time = 3.
15 * Example 2:
16 * <img alt="" src="https://assets.leetcode.com/uploads/2021/12/13/balloon2.jpg" style="width: 244px; height: 243px;" />
17 * Input: colors = "abc", neededTime = [1,2,3]
18 * Output: 0
19 * Explanation: The rope is already colorful. Bob does not need to remove any balloons from the rope.
20 * 
21 * Example 3:
22 * <img alt="" src="https://assets.leetcode.com/uploads/2021/12/13/balloon3.jpg" style="width: 404px; height: 243px;" />
23 * Input: colors = "aabaa", neededTime = [1,2,3,4,1]
24 * Output: 2
25 * Explanation: Bob will remove the ballons at indices 0 and 4. Each ballon takes 1 second to remove.
26 * There are no longer two consecutive balloons of the same color. Total time = 1 + 1 = 2.
27 * 
28 *  
29 * Constraints:
30 * 
31 * 	n == colors.length == neededTime.length
32 * 	1 <= n <= 10^5
33 * 	1 <= neededTime[i] <= 10^4
34 * 	colors contains only lowercase English letters.
35 * 
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/minimum-time-to-make-rope-colorful/
40// discuss: https://leetcode.com/problems/minimum-time-to-make-rope-colorful/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45    pub fn min_cost(colors: String, needed_time: Vec<i32>) -> i32 {
46        0
47    }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn test_1578() {
58    }
59}
60


Back
© 2025 bowen.ge All Rights Reserved.