Minimum Time to Make Rope Colorful
给一个字符串和一个对应的数组, 求字符串每个不相邻的不同颜色的最大值.
class Solution {
public int minCost(String colors, int[] neededTime) {
int sum = 0;
int n = neededTime.length;
for(int m : neededTime){
sum += m;
}
if(n == 1)
return neededTime[0];
char cur = colors.charAt(0);
int cur_max = neededTime[0];
int maxSum = 0;
for(int i = 1; i < neededTime.length; i++) {
char c = colors.charAt(i);
if(c == cur){
cur_max = Math.max(cur_max, neededTime[i]);
}
else
{
cur = c;
maxSum += cur_max;
cur_max = neededTime[i];
}
}
return sum - maxSum - cur_max;
}
}