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.
Didn't see any JavaScript solutions, so here comes mine:
functionactivityNotifications(expenditure,d){// Number of notificationsletn=0// Set midpoints for median calculationlet[i1,i2]=[Math.floor((d-1)/2),Math.ceil((d-1)/2)]letm1,m2,m// Initialize count sorted subarrayletcs=newArray(201).fill(0)for(leti=d-1;i>=0;i--)cs[expenditure[i]]++// Iterate through expendituresfor(leti=d,l=expenditure.length;i<l;i++){// Find medianfor(letj=0,k=0;k<=i1;k+=cs[j],j++)m1=jfor(letj=0,k=0;k<=i2;k+=cs[j],j++)m2=jletm=(m1+m2)/2// Check if notification is givenif(expenditure[i]>=m*2)n++// Replace subarray elementscs[expenditure[i-d]]--cs[expenditure[i]]++}returnn}
It can be optimized by simplifying some redundant calculations, but I wrote it this way to make the code easier to follow and understand ;)
Fraudulent Activity Notifications
You are viewing a single comment's thread. Return to all comments →
Didn't see any JavaScript solutions, so here comes mine:
It can be optimized by simplifying some redundant calculations, but I wrote it this way to make the code easier to follow and understand ;)