[Hacker Rank] Fraudulent Activity Notifications

Fraudulent Activity Notifications

  • Time : O(nlogn)
  • Space : O(d)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int activityNotifications(vector<int> A, int d) {
int res = 0, n = A.size();
multiset<int> ms(begin(A), begin(A) + d);
auto m = next(begin(ms), d / 2);

for(int i = d; i < n; i++) {
int median = *m + *prev(m, 1 - (d&1));
if(A[i] >= median) res++;

ms.insert(A[i]);
if(A[i] < *m) m--;
if(A[i-d] <= *m) m++;
ms.erase(ms.find(A[i-d]));
}
return res;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/06/12/PS/HackerRank/fraudulent-activity-notifications/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.