We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
JS - use of array-functions: filter, sort & reduce
functionluckBalance(k,contests){// Write your code hereconstunimportantArr=contests.filter(c=>c[1]===0);constimportantArr=contests.filter(c=>c[1]===1);// sort -> from least important to max importantimportantArr.sort((a,b)=>a[0]-b[0]<0?1:-1);// we lose all unimportant constests for max luckconstluckU=unimportantArr.reduce((a,c)=>a+c[0],0);// the least important ones (lower than k) we win, else we lose to gain luckconstluckI=importantArr.reduce((a,c,idx)=>idx>=k?a-c[0]:a+c[0],0);returnluckU+luckI;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Luck Balance
You are viewing a single comment's thread. Return to all comments →
JS - use of array-functions: filter, sort & reduce