java: add “1578. Minimum Time to Make Rope Colorful”
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
fdc2539ddb
commit
10369a5031
1 changed files with 22 additions and 0 deletions
22
java/minimum-time-to-make-rope-colorful.java
Normal file
22
java/minimum-time-to-make-rope-colorful.java
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
class Solution {
|
||||||
|
public int minCost(String colors, int[] neededTime) {
|
||||||
|
int[] dp = new int[colors.length() + 1];
|
||||||
|
Arrays.fill(dp, 0);
|
||||||
|
char previousColor = 0;
|
||||||
|
int previousTime = 0;
|
||||||
|
|
||||||
|
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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dp[colors.length()];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue