Ad
  • Custom User Avatar

    Updated solution, the do-while was silly ;)

    export class G964 {
        public static nbMonths(startPriceOld: number, startPriceNew: number, savingPerMonth: number, percentLossByMonth: number): number[] {
            // start with month 0 and accumulate savings after the first month
            let currentMonth: number = 0;
            let savings: number = 0;
    
            // we can already check if we have enough capital to buy a new car
            // check if balance is >= 0 after 'selling' the old car + savings and 'buying' a new one
            while ((savings + startPriceOld - startPriceNew) < 0) {
              // update start prices
              let currentPercent = (100-percentLossByMonth) / 100;
              startPriceOld = startPriceOld * currentPercent;
              startPriceNew = startPriceNew * currentPercent;
    
              // save some money and update percentages
              savings += savingPerMonth;
              percentLossByMonth += (++currentMonth % 2) > 0 ? 0.5 : 0;
            }
    
            return [currentMonth, Math.round(savings + startPriceOld - startPriceNew)];
        }
    }
    
  • Default User Avatar

    Wrong since all we need is to verify for a1.

  • Custom User Avatar

    only works as long as a1.length >= a2.length