I am trying to solve a Sorting Problem in Python3 from HackerRank: https://www.hackerrank.com/challenges/fraudulent-activity-notifications/problem
This problem requires finding the median for every sub list on a running base.
My code passes for the Sample Test Cases but not entirely for the actual Test Cases due to timeout termination. I suspect using sort() every time to find the median is causing the time lag.
How can I improve my code?
def activityNotifications(expenditure, d):
totalDays = len(expenditure)
notified = 0
for x in range(d, totalDays):
check = expenditure[x-d:x]
check.sort()
if d % 2 == 0:
median = (check[int(d/2)] + check[int((d-2)/2)])/2
else:
median = check[int((d-1)/2)]
if expenditure[x] >= median * 2:
notified += 1
return notified