Minimum Time to Make Rope Colorful LeetCode solution in Java

1
class Solution {
2
public int minCost(String colors, int[] neededTime) {
3
return minCost(colors, neededTime, 0, neededTime.length - 1);
4
}
5

6
public int minCost(String colors, int[] neededTime, int start, int end) {
7
if (start == end) {
8
return 0;
9
}
10

11
int mid = (start + end) / 2;
12
int lEnd = mid;
13
int rStart = mid + 1;
14
int t1 = minCost(colors, neededTime, start, lEnd);
15
int t2 = minCost(colors, neededTime, rStart, end);
16

17
while (neededTime[lEnd] < 0 && lEnd >= start) {
18
--lEnd;
19
}
20
while (neededTime[rStart] < 0 && rStart <= end) {
21
++rStart;
22
}
23

24
if (colors.charAt(lEnd) != colors.charAt(rStart)) {
25
return t1 + t2;
26
}
27

28
int removeTime = 0;
29
if (neededTime[lEnd] <= neededTime[rStart]) {
30
removeTime = neededTime[lEnd];
31
neededTime[lEnd] *= -1;
32
} else {
33
removeTime = neededTime[rStart];
34
neededTime[rStart] *= -1;
35
}
36

37
return t1 + t2 + removeTime;
38
}
39
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0