2023-12-28 01:04:42 +01:00
|
|
|
class Solution {
|
2024-03-02 21:01:53 +01:00
|
|
|
public int minCost(String colors, int[] neededTime) {
|
|
|
|
int[] dp = new int[colors.length() + 1];
|
|
|
|
Arrays.fill(dp, 0);
|
|
|
|
char previousColor = 0;
|
|
|
|
int previousTime = 0;
|
2023-12-28 01:04:42 +01:00
|
|
|
|
2024-03-02 21:01:53 +01:00
|
|
|
for (int i = 1; i <= colors.length(); ++i) {
|
|
|
|
if (colors.charAt(i - 1) == previousColor) {
|
|
|
|
dp[i] = dp[i - 1] + Math.min(previousTime, neededTime[i - 1]);
|
|
|
|
previousTime = Math.max(previousTime, neededTime[i - 1]);
|
|
|
|
} else {
|
|
|
|
dp[i] = dp[i - 1];
|
|
|
|
previousColor = colors.charAt(i - 1);
|
|
|
|
previousTime = neededTime[i - 1];
|
|
|
|
}
|
2023-12-28 01:04:42 +01:00
|
|
|
}
|
2024-03-02 21:01:53 +01:00
|
|
|
|
|
|
|
return dp[colors.length()];
|
|
|
|
}
|
2023-12-28 01:04:42 +01:00
|
|
|
}
|