Untitled

 avatar
unknown
ruby
a year ago
669 B
8
Indexable
module PatchedArray
    refine Array do
      def median
        return nil if empty?
  
        arr = sort
        len = size
  
        (arr[(len - 1) / 2] + arr[len / 2]) / 2.0
      end
    end
end
  
using PatchedArray
  
def activityNotifications(expenditure, d)
    notifications_count = 0
  
    expenditure.each_with_index do |exp, idx|
      # ignore first d days because of data collecting
      next if idx < d
  
      # collect expenditures for d days before d+1 day
      exps = expenditure[idx - d..idx - 1]
  
      # increase notifications_count if needed
      notifications_count += 1 if exp >= 2 * exps.median
    end
  
    notifications_count
end
Editor is loading...
Leave a Comment