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.
O(n*d) solution with Python bisect. Clean, readable code.
importbisectdefactivityNotifications(expenditure,d):warns=0# Initialize sorted trailing windowtrailing=sorted(expenditure[:d])fortodayinrange(d,len(expenditure)):# Get median from sorted trailing listifd%2==1:median=trailing[d// 2]else:median=(trailing[d// 2] + trailing[d // 2 - 1]) / 2ifexpenditure[today]>=2*median:warns+=1# Slide the window# Remove the element going out of the windowold_value=expenditure[today-d]deltrailing[bisect.bisect_left(trailing,old_value)]# Insert the new value to maintain sorted orderbisect.insort(trailing,expenditure[today])returnwarns
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 →
O(n*d) solution with Python bisect. Clean, readable code.