You are viewing a single comment's thread. Return to all comments →
Solved it in C# using a SortedList, inserting and removing using BinarySearch
internal class SortedList<T> { private readonly List<T> src; public SortedList() => this.src = new List<T>(); public int Count => src.Count; public void Add(T item) { int insertIndex = src.BinarySearch(item); if (insertIndex < 0) insertIndex = ~insertIndex; src.Insert(insertIndex, item); } public T this[int index] => src[index]; public bool Remove(T item) { int index = src.BinarySearch(item); if (index < 0) return false; src.RemoveAt(index); return true; } } public static double Median(SortedList<int> ar) { int n = ar.Count; double median = (ar[n / 2] + ar[(n - 1) / 2]) / 2.0; return median; } public static int ActivityNotifications(int[] expenditure, int d) { int notifications = 0; var previousExpenditures = new SortedList<int>(); for (int i = 0; i < d; i++) //Add initial expenditures { previousExpenditures.Add(expenditure[i]); } for (int i = d; i < expenditure.Length; i++) { if (expenditure[i] >= 2 * Median(previousExpenditures)) { notifications++; } previousExpenditures.Remove(expenditure[i - d]); previousExpenditures.Add(expenditure[i]); } return notifications; }
Seems like cookies are disabled on this browser, please enable them to open this website
Fraudulent Activity Notifications
You are viewing a single comment's thread. Return to all comments →
Solved it in C# using a SortedList, inserting and removing using BinarySearch