Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function minCost(colors: string, neededTime: number[]): number {
- let lastColor = 'A';
- let lastIx = -1;
- let timeSoFar = 0;
- for(let ix = 0; ix < colors.length; ix++) {
- if (colors[ix] === lastColor) {
- // remove this one or lastIx, depending on which has a smaller cost
- if (neededTime[ix] > neededTime[lastIx]) {
- // remove previous
- timeSoFar += neededTime[lastIx];
- lastIx = ix;
- } else {
- // remove this
- timeSoFar += neededTime[ix];
- }
- } else {
- lastColor = colors[ix];
- lastIx = ix;
- }
- }
- return timeSoFar;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement