Fraudulent Activity Notifications

Sort by

recency

|

1182 Discussions

|

  • + 0 comments

    Here is a Java solution with sorted arrayList and sliding window:

    public static int activityNotifications(List<Integer> expenditure, int d) {
            var list = new ArrayList<Integer>();
            int notifications = 0;
    
            Function<List<Integer>, Double> medFunc;
            if (d % 2 == 0) {
                medFunc = (arr) -> ((arr.get(d / 2) + arr.get(d / 2 - 1)) / 2.);
            } else {
                medFunc = (arr) -> (double) arr.get(d / 2);
            }
    
            var indexToRemove = 0;
            for (var i = 0; i < expenditure.size(); i++) {
                if (i < d) {
                    add(expenditure.get(i), list);
                } else {
                    double med = medFunc.apply(list);
                    if (med * 2 <= expenditure.get(i)) {
                        notifications++;
                    }
    
                    if (i < expenditure.size() - 1) {
                        add(expenditure.get(i), list);
                        remove(expenditure.get(indexToRemove), list);
                        indexToRemove++;
                    }
                }
            }
    
            return notifications;
        }
    
        public static void add(Integer value, List<Integer> list) {
            int index = Collections.binarySearch(list, value);
    
            // If value is not found, binarySearch returns (-(insertion point) - 1)
            if (index < 0) {
                index = -(index + 1);
            }
    
            list.add(index, value);
        }
    
        public static void remove(Integer value, List<Integer> list) {
            int index = Collections.binarySearch(list, value);
            if (index >= 0) {
                list.remove(index);
            }
        }
    
  • + 0 comments
    • my solution for JS, worked for me on all test-cases
    • with explanation due to JS being very slow on high-load-operations
    function activityNotifications(expenditure, d) {
      // Write your code here
      let notificationCounter = 0;
      const middle = Math.ceil(d / 2);
      
      // sort first trailing days, use this as base for future operations and to calculate the median
      const sortedTrailingDays = expenditure
        .slice(0, d)
        .sort((a, b) => (b < a ? 0 : -1));
    
      for (let i = d; i < expenditure.length; i++) {
        const spentThisDay = expenditure[i];
    
        if (i > d) { // ignore first iteration, trailings days are sorted once before the loop
          /**
           * 2 operations on the sorted array of trailing day expenditures
           *    1. remove the expenditure of the day that dropped out of the trailing days
           *    2. add the expenditure of the previous day to the trailing days at the correct position
           * 
           * this is way faster than sorting the whole array on every iteration 
           * with the js-array-sort method (as done above the for-loop)
           *  -> this way the function was fast enough to succeed all test-cases, 
           *     even in slow-ass single-CPU-javascript :D
           */
          // remove "dropped out" number/day
          sortedTrailingDays.splice(
            sortedTrailingDays.indexOf(expenditure[i - d - 1]),
            1
          );
          // insert "previous-day-expenditure" to sorted array at the correct position
          const spentDayBefore = expenditure[i - 1];
          if (spentDayBefore <= sortedTrailingDays[0]) {
            sortedTrailingDays.unshift(spentDayBefore); // first pos
          } else if (
            spentDayBefore >= sortedTrailingDays[sortedTrailingDays.length - 1]
          ) {
            sortedTrailingDays.push(spentDayBefore);  // last pos
          } else {
            // find first higher number -> insert there
            const idx = sortedTrailingDays.indexOf(
              sortedTrailingDays.find((n) => n > spentDayBefore) // most likely to slow for testcase-5
            );
            sortedTrailingDays.splice(idx, 0, spentDayBefore);
          }
        }
        
        // calculate median
        const median = d % 2 === 0 ? 
            (sortedTrailingDays[middle] + sortedTrailingDays[middle - 1]) / 2 
            : sortedTrailingDays[middle - 1];
    
        if (spentThisDay >= median * 2) {
          notificationCounter++;
        }
      }
    
      return notificationCounter;
    }
    
  • + 0 comments
    #include <bits/stdc++.h>
    
    using namespace std;
    
    vector<string> split_string(string);
    double getMedian(vector<int> occur, int elements);
    // Runs in O(n*d)
    int activityNotifications(vector<int> exp, int d) {
        
        int n;
        int count;
        int old_exp;
        vector<int>  occur(201);
        queue <int>  transaction_queue;
        double median;//i have some confusion in the code if there is a function that is activityNotificatiion give output of two function & logic of two function then what's "give the error" here the code of two logic in two different function.
        
        count = 0;
        n = exp.size();
        
        for(int i =0; i< d;i++)
            occur[exp[i]]++;
        
        
        for(int i =0; i< d;i++)
            transaction_queue.push(exp[i]);
        
        
        for(int i = d; i< n;i++){
            median = getMedian(occur, d);
            if(exp[i] >= 2.0*median){
                count++;
            }
            old_exp = transaction_queue.front();
            occur[old_exp]--;
            transaction_queue.pop();
            
            transaction_queue.push(exp[i]);
            occur[exp[i]]++;
        }
        
        
        return count;
    }
    
    double getMedian(vector<int> occur, int elements){
        
        int i;
        int j;
        bool isEven;
        int count;
        double median;
        
        i = 0;
        isEven = (elements % 2 == 0);
        count = 0;
        
        if(isEven){
            count = elements/2;
            
            while(count > 0){
                count -= occur[i];
                i++;
            }
            i--;
            if(count < 0){
                median = (double) i;
            }else{
                j = i+1;
                while(occur[j] == 0){
                    j++;
                }
                
                median = (double) (i+j)/(2);
            }
        }else{
            count = floor(elements/2);
            while(count >= 0){
                count -= occur[i];
                i++;
            }
            i--;
            
            median = (double) (i);
        }
        
        return median;
    }
    
        return splits;
    }
    
  • + 0 comments

    include

    using namespace std;

    int test(const vector &a, int n, int d) { int res = 0; vectorb(a.begin(), a.begin() + d); sort(b.begin(), b.end()); for(int i = d; i < n; i++) { int sum = 0; if(d % 2 == 1) { sum = 2 * b[d / 2]; } else { sum = b[d / 2] + b[d / 2 - 1]; } if(a[i] >= sum) { res++; } b.erase(lower_bound(b.begin(), b.end(), a[i - d])); b.insert(lower_bound(b.begin(), b.end(), a[i]), a[i]); } return res; }

    int main() { int n, d; cin >> n >> d; vector a(n); for(int i = 0; i < n; i++) { cin >> a[i]; } cout << test(a, n, d) << endl; return 0; }

  • + 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