- Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathMinimumTimeToColorRope.java
40 lines (38 loc) · 1.11 KB
/
MinimumTimeToColorRope.java
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
/*
*
* Minimum Time to make the Rope Colorful.
* Leetcode Question - 1578.
*
* In this code I have used the Greedy Approach To solve this question.
*/
publicclassMinimumTimeToColorRope {
publicstaticvoidmain(String[] args) {
inttime[] = {1,2,3,4,5}; //Time in Seconds
Stringcolors = "abaac"; //Colors Sequence
System.out.println(minCost(colors, time)); //Total Time Taken to make the rope colorfull
}
publicstaticintminCost(Stringcolors, int[] neededTime) {
intsec = 0;
charc = ' ';
intcp = 0;
for(inti=0;i<colors.length();i++)
{
if(colors.charAt(i) == c)
{
if(cp < neededTime[i])
{
sec += cp;
cp = neededTime[i];
}
else
sec += neededTime[i];
}
else
{
c = colors.charAt(i);
cp = neededTime[i];
}
}
returnsec;
}
}