Skip to content

Commit 7a76b54

Browse files
Create 1578-minimum-time-to-make-rope-colorful.java
1 parent 489140e commit 7a76b54

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*-------------------------------
2+
Time Complexity: O(n)
3+
Space Complexity: O(1)
4+
--------------------------------*/
5+
class Solution {
6+
public int minCost(String colors, int[] neededTime) {
7+
int res = 0, l = 0;
8+
9+
for(int r = 1; r < colors.length(); r++){
10+
if(colors.charAt(l) == colors.charAt(r)){
11+
if(neededTime[l] < neededTime[r]){
12+
res += neededTime[l];
13+
l = r;
14+
}
15+
else
16+
res += neededTime[r];
17+
}
18+
else
19+
l = r;
20+
}
21+
return res;
22+
}
23+
}

0 commit comments

Comments
 (0)