Advertisement
jorupp

https://leetcode.com/problems/minimum-time-to-make-rope-colorful

Mar 22nd, 2023
807
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function minCost(colors: string, neededTime: number[]): number {
  2.     let lastColor = 'A';
  3.     let lastIx = -1;
  4.     let timeSoFar = 0;
  5.     for(let ix = 0; ix < colors.length; ix++) {
  6.         if (colors[ix] === lastColor) {
  7.             // remove this one or lastIx, depending on which has a smaller cost
  8.             if (neededTime[ix] > neededTime[lastIx]) {
  9.                 // remove previous
  10.                 timeSoFar += neededTime[lastIx];
  11.                 lastIx = ix;
  12.             } else {
  13.                 // remove this
  14.                 timeSoFar += neededTime[ix];
  15.             }
  16.         } else {
  17.             lastColor = colors[ix];
  18.             lastIx = ix;
  19.         }
  20.     }
  21.     return timeSoFar;
  22. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement