Fraudulent Activity Notifications

  • + 0 comments

    Python3 using bisect library

    from bisect import bisect, insort
    def activityNotifications(expenditures, d):
        res = 0
        vals = sorted(expenditures[:d])
        for first_val, cur_val in zip(expenditures, expenditures[d:]):    
            if d % 2:
                median2 = 2 * vals[d//2]
            else:
                median2 = vals[d//2] + vals[d//2-1]
            if cur_val >= median2:
                res += 1
            vals.pop(bisect(vals, first_val) - 1)
            insort(vals, cur_val)
        return res