Может кто-нибудь помочь с улучшением внутреннего для l oop? Я пробовал много разных способов, и я не мог получить это быстрее. У меня была проблема с HackerRank, и я продолжал получать сообщения о нескольких тестовых случаях, которые тест не завершил за отведенное время.
function minSum(num, k) {
console.time("totaltime");
// Write your code here
for (var i = 0; i < k; i++) {
let maxValue = num[0];
let maxIndex = 0;
//maxIndex = num.indexOf(Math.max(...num)); // 3517.743ms
for (let j = 1; j < num.length; j++) { // 661.414ms
if (num[j] > maxValue) {
maxIndex = j;
maxValue = num[j];
}
}
num[maxIndex] = Math.ceil( num[maxIndex] / 2);
}
console.timeEnd("totaltime");
return num.reduce((pv, cv) => pv + cv, 0);
}
const arr = [6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99, 100, 105, 1000, 3000, 5000, 67,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99, 100, 105, 1000, 3000, 5000, 67,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99, 100, 105, 1000, 3000, 5000, 67,
6, 4, 3, 5, 1, 9, 2, 10, 25, 100, 39, 38 ,17, 0, 44, 99, 100, 105, 1000, 3000, 5000, 67
];
const k=950000;
console.log(minSum(arr, k));