You are viewing a single comment's thread. Return to all comments →
javaScript solution
function luckBalance(k, contests) { let important = contests.filter(ar => ar[1] === 1).length; let sumAll = contests.reduce((a, b) => a+b[0],0); let sorted = contests.sort((a, b) => a[0] - b[0]) let win = important-k >=0 ?important-k : 0 let min = 0 for(let i=0; i<sorted.length; i++){ if(win === 0) break; if(sorted[i][1] === 0)continue; min += sorted[i][0]; win-- } return sumAll - (2*min); }
There seems to be a lot of loops, with this solutions is the time complexity a: O(n*3) + O(m) ... n being constests - m being sorted ... ?
how about this for a change ?
function luckBalance(k, contests) { const importantContestsVals = [] let val = 0; for (let i = 0; i < contests.length; i++) { if (contests[i][1]) { importantContestsVals.push(contests[i][0]) } else { val += contests[i][0] } } importantContestsVals.sort((a, b) => a - b) const negativeArr = importantContestsVals.splice(0, importantContestsVals.length - k) const largestArr = negativeArr.length >= importantContestsVals.length ? negativeArr.length : importantContestsVals.length for (let j = 0; j < largestArr; j++) { if (negativeArr[j]) { val -= negativeArr[j] } if (importantContestsVals[j]) { val += importantContestsVals[j] } } return val }
Luck Balance
You are viewing a single comment's thread. Return to all comments →
javaScript solution
There seems to be a lot of loops, with this solutions is the time complexity a: O(n*3) + O(m) ... n being constests - m being sorted ... ?
how about this for a change ?