Alice 把 n 个气球排列在一根绳子上。给你一个下标从 0 开始的字符串 colors ,其中 colors[i] 是第 i 个气球的颜色。
Alice 想要把绳子装扮成 彩色 ,且她不希望两个连续的气球涂着相同的颜色,所以她喊来 Bob 帮忙。Bob 可以从绳子上移除一些气球使绳子变成 彩色 。给你一个下标从 0 开始的整数数组 neededTime ,其中 neededTime[i] 是 Bob 从绳子上移除第 i 个气球需要的时间(以秒为单位)。
classSolution { public: intminCost(string colors, vector<int>& neededTime){ int i = 0, len = colors.length(); int ret = 0; while (i < len) { char ch = colors[i]; int maxValue = 0; int sum = 0;
while (i < len && colors[i] == ch) { maxValue = max(maxValue, neededTime[i]); sum += neededTime[i]; i++; } ret += sum - maxValue; } return ret; } };
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { publicintminCost(String colors, int[] neededTime) { inti=0, len = colors.length(); intret=0; while (i < len) { charch= colors.charAt(i); intmaxValue=0; intsum=0;
while (i < len && colors.charAt(i) == ch) { maxValue = Math.max(maxValue, neededTime[i]); sum += neededTime[i]; i++; } ret += sum - maxValue; } return ret; } }
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
publicclassSolution { publicintMinCost(string colors, int[] neededTime) { int i = 0, len = colors.Length; int ret = 0; while (i < len) { char ch = colors[i]; int maxValue = 0; int sum = 0;
while (i < len && colors[i] == ch) { maxValue = Math.Max(maxValue, neededTime[i]); sum += neededTime[i]; i++; } ret += sum - maxValue; } return ret; } }
[sol1-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution: defminCost(self, colors: str, neededTime: List[int]) -> int: i = 0 length = len(colors) ret = 0
while i < length: ch = colors[i] maxValue = 0 total = 0
while i < length and colors[i] == ch: maxValue = max(maxValue, neededTime[i]) total += neededTime[i] i += 1 ret += total - maxValue return ret
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
intminCost(char* colors, int* neededTime, int neededTimeSize) { int i = 0; int ret = 0; while (i < neededTimeSize) { char ch = colors[i]; int maxValue = 0; int sum = 0;
while (i < neededTimeSize && colors[i] == ch) { maxValue = fmax(maxValue, neededTime[i]); sum += neededTime[i]; i++; } ret += sum - maxValue; } return ret; }