Yazı Kafası Site Logosu
Maximum Cost of Laptop Count
Maximum Cost of Laptop Count 168364

Question IMG

const cost = [2, 5, 3, 11, 1];

const label = ['legal', 'illegal', 'legal', 'illegal', 'legal'];

//dailyCount

function maxCost(cost, label, dailyCount) {

  let combined = cost.map(function (e, i) {

    return [e, label[i]];

  });

  let max = 0;

  let totalCost = 0;

  let legalIndex = 0;

  for (let i = 0; i < combined.length; i++) {

    totalCost += combined[i][0];

    if (combined[i][1] === 'legal') {

      legalIndex++;

    }

    if (legalIndex === dailyCount) {

      max = Math.max(max, totalCost);

      totalCost = 0;

      legalIndex = 0;

    }

  }

  return max;

}

console.log(maxCost(cost, label, 2));

2461 165859

Benzer Yazılar