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.
my solution for JS, worked for me on all test-cases
with explanation due to JS being very slow on high-load-operations
functionactivityNotifications(expenditure,d){// Write your code hereletnotificationCounter=0;constmiddle=Math.ceil(d/2);// sort first trailing days, use this as base for future operations and to calculate the medianconstsortedTrailingDays=expenditure.slice(0,d).sort((a,b)=>(b<a?0:-1));for(leti=d;i<expenditure.length;i++){constspentThisDay=expenditure[i];if(i>d){// ignore first iteration, trailings days are sorted once before the loop/** * 2 operations on the sorted array of trailing day expenditures * 1. remove the expenditure of the day that dropped out of the trailing days * 2. add the expenditure of the previous day to the trailing days at the correct position * * this is way faster than sorting the whole array on every iteration * with the js-array-sort method (as done above the for-loop) * -> this way the function was fast enough to succeed all test-cases, * even in slow-ass single-CPU-javascript :D */// remove "dropped out" number/daysortedTrailingDays.splice(sortedTrailingDays.indexOf(expenditure[i-d-1]),1);// insert "previous-day-expenditure" to sorted array at the correct positionconstspentDayBefore=expenditure[i-1];if(spentDayBefore<=sortedTrailingDays[0]){sortedTrailingDays.unshift(spentDayBefore);// first pos}elseif(spentDayBefore>=sortedTrailingDays[sortedTrailingDays.length-1]){sortedTrailingDays.push(spentDayBefore);// last pos}else{// find first higher number -> insert thereconstidx=sortedTrailingDays.indexOf(sortedTrailingDays.find((n)=>n>spentDayBefore)// most likely to slow for testcase-5);sortedTrailingDays.splice(idx,0,spentDayBefore);}}// calculate medianconstmedian=d%2===0?(sortedTrailingDays[middle]+sortedTrailingDays[middle-1])/2:sortedTrailingDays[middle-1];if(spentThisDay>=median*2){notificationCounter++;}}returnnotificationCounter;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Fraudulent Activity Notifications
You are viewing a single comment's thread. Return to all comments →