forked from silent-killer-11/Hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Minimum Time to Make Rope Colorful
49 lines (36 loc) · 1.34 KB
/
Minimum Time to Make Rope Colorful
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
47
48
49
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.
SOLUTION:
class Solution {
public:
int minCost(string colors, vector<int>& neededTime) {
int mincost = 0;
int i = 0;
int j = 1;
while(i < colors.size()-1)
{
if(colors[i] == colors[j])
{
if(neededTime[i] <= neededTime[j])
{
mincost += neededTime[i];
i = j;
j++;
}
else
{
mincost += neededTime[j];
// i = j;
j++;
}
}
else
{
i = j;
j++;
}
}
return mincost;
}
};