Skip to content

Commit 7cd9264

Browse files
committed
1578. Minimum Time to Make Rope Colorful: AC
1 parent 4a6f0fa commit 7cd9264

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1190,3 +1190,4 @@ mod s1574_shortest_subarray_to_be_removed_to_make_array_sorted;
11901190
mod s1575_count_all_possible_routes;
11911191
mod s1576_replace_all_s_to_avoid_consecutive_repeating_characters;
11921192
mod s1577_number_of_ways_where_square_of_number_is_equal_to_product_of_two_numbers;
1193+
mod s1578_minimum_time_to_make_rope_colorful;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 balloons at indices 0 and 4. Each balloons 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+
*/
37+
pub 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+
44+
impl Solution {
45+
pub fn min_cost(colors: String, needed_time: Vec<i32>) -> i32 {
46+
let (mut result, mut l) = (0, 0);
47+
48+
for r in 1..colors.len() {
49+
if colors[r..=r] != colors[l..=l] {
50+
l = r;
51+
} else if needed_time[r] < needed_time[l] {
52+
result += needed_time[r];
53+
} else {
54+
result += needed_time[l];
55+
l = r;
56+
}
57+
}
58+
59+
result
60+
}
61+
}
62+
63+
// submission codes end
64+
65+
#[cfg(test)]
66+
mod tests {
67+
use super::*;
68+
69+
#[test]
70+
fn test_1578_example_1() {
71+
let colors = "abaac".to_string();
72+
let needed_time = vec![1, 2, 3, 4, 5];
73+
74+
let result = 3;
75+
76+
assert_eq!(Solution::min_cost(colors, needed_time), result);
77+
}
78+
79+
#[test]
80+
fn test_1578_example_2() {
81+
let colors = "abc".to_string();
82+
let needed_time = vec![1, 2, 3];
83+
84+
let result = 0;
85+
86+
assert_eq!(Solution::min_cost(colors, needed_time), result);
87+
}
88+
89+
#[test]
90+
fn test_1578_example_3() {
91+
let colors = "aabaa".to_string();
92+
let needed_time = vec![1, 2, 3, 4, 1];
93+
94+
let result = 2;
95+
96+
assert_eq!(Solution::min_cost(colors, needed_time), result);
97+
}
98+
}

0 commit comments

Comments
 (0)