Skip to content

Commit 766dfa1

Browse files
Merge pull request codemistic#456 from Ayushagr145/main
Minimum Time Taken To Make the Rope Colorful.
2 parents c73088d + 61d930a commit 766dfa1

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Java/MinimumTimeToColorRope.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
*
3+
* Minimum Time to make the Rope Colorful.
4+
* Leetcode Question - 1578.
5+
*
6+
* In this code I have used the Greedy Approach To solve this question.
7+
*/
8+
9+
10+
public class MinimumTimeToColorRope {
11+
public static void main(String[] args) {
12+
int time[] = {1,2,3,4,5}; //Time in Seconds
13+
String colors = "abaac"; //Colors Sequence
14+
System.out.println(minCost(colors, time)); //Total Time Taken to make the rope colorfull
15+
}
16+
public static int minCost(String colors, int[] neededTime) {
17+
int sec = 0;
18+
char c = ' ';
19+
int cp = 0;
20+
for(int i=0;i<colors.length();i++)
21+
{
22+
if(colors.charAt(i) == c)
23+
{
24+
if(cp < neededTime[i])
25+
{
26+
sec += cp;
27+
cp = neededTime[i];
28+
}
29+
else
30+
sec += neededTime[i];
31+
}
32+
else
33+
{
34+
c = colors.charAt(i);
35+
cp = neededTime[i];
36+
}
37+
}
38+
return sec;
39+
}
40+
}

0 commit comments

Comments
 (0)