Fraudulent Activity Notifications

Sort by

recency

|

1191 Discussions

|

  • + 0 comments

    Locksmith York provides expert lock repair, replacement, and emergency services, keeping homes, businesses, and vehicles secure. With skilled technicians and quick response times, they deliver reliable solutions tailored to customer needs. Known for professionalism and trust, their team ensures peace of mind in every situation. Similarly, Fraudulent Activity Notifications play a crucial role in protecting individuals and businesses, alerting users to suspicious behavior and helping safeguard sensitive information from potential cyber threats.

  • + 0 comments

    Calculate median on the fly utilizing the fact that all expenditures are below 200 (reducing the problem to find median in Counter of integers)

    def activityNotifications(expenditure, d):
        counts = [0] * 200
        exp = expenditure
    
        for x in exp[:d]:
            counts[x] += 1
    
        def M():
            total_left = 0
            for l in range(200):
                total_left += counts[l]
                if total_left > d - total_left:
                    return l
                elif total_left == d - total_left:
                    r = l + 1
                    while not counts[r]: 
                        r += 1
                    return (l + r) / 2.
    
        res = 0
        for i in range(d, len(exp)):
            res += (exp[i] >= M() * 2)
            counts[exp[i-d]] -= 1
            counts[exp[i]] += 1
            
        return res
    
  • + 0 comments

    import java.io.; import java.util.; import java.util.stream.*;

    class Result {

    public static int activityNotifications(List<Integer> expenditure, int d) {
        int notifications = 0;
        int[] count = new int[201]; // expenditure[i] <= 200
    
        for (int i = 0; i < d; i++) {
            count[expenditure.get(i)]++;
        }
    
        for (int i = d; i < expenditure.size(); i++) {
            double median = getMedian(count, d);
            if (expenditure.get(i) >= 2 * median) {
                notifications++;
            }
    
            // Slide the window
            count[expenditure.get(i - d)]--;
            count[expenditure.get(i)]++;
        }
    
        return notifications;
    }
    
    private static double getMedian(int[] count, int d) {
        int sum = 0;
        if (d % 2 == 1) {
            int mid = d / 2 + 1;
            for (int i = 0; i < count.length; i++) {
                sum += count[i];
                if (sum >= mid) return i;
            }
        } else {
            int mid1 = d / 2;
            int mid2 = mid1 + 1;
            int m1 = -1, m2 = -1;
            for (int i = 0; i < count.length; i++) {
                sum += count[i];
                if (sum >= mid1 && m1 == -1) m1 = i;
                if (sum >= mid2) {
                    m2 = i;
                    break;
                }
            }
            return (m1 + m2) / 2.0;
        }
        return 0;
    }
    

    }

    public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        String[] firstMultipleInput = bufferedReader.readLine().trim().split(" ");
        int n = Integer.parseInt(firstMultipleInput[0]);
        int d = Integer.parseInt(firstMultipleInput[1]);
    
        List<Integer> expenditure = Arrays.stream(bufferedReader.readLine().trim().split(" "))
            .map(Integer::parseInt)
            .collect(Collectors.toList());
    
        int result = Result.activityNotifications(expenditure, d);
    
        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();
    
        bufferedReader.close();
        bufferedWriter.close();
    }
    

    }

  • + 0 comments

    O(n*d) solution with Python bisect. Clean, readable code.

    import bisect
    
    def activityNotifications(expenditure, d):
        warns = 0
        # Initialize sorted trailing window
        trailing = sorted(expenditure[:d])
    
        for today in range(d, len(expenditure)):
            # Get median from sorted trailing list
            if d % 2 == 1:
                median = trailing[d // 2]
            else:
                median = (trailing[d // 2] + trailing[d // 2 - 1]) / 2
    
            if expenditure[today] >= 2 * median:
                warns += 1
    
            # Slide the window
            # Remove the element going out of the window
            old_value = expenditure[today - d]
            del trailing[bisect.bisect_left(trailing, old_value)]
    
            # Insert the new value to maintain sorted order
            bisect.insort(trailing, expenditure[today])
    
        return warns
                
            
    
  • + 0 comments

    My Kotlin version, which sorts the first d elements once, and then adopts binary search to remove/insert old/new elements as the window slides;

    fun insertInSortedList(items: MutableList<Int>, newElement: Int) {
        val index = items.binarySearch(newElement).let { if (it < 0) -it - 1 else it }
        items.add(index, newElement)
    }
    
    fun removeItemFromSortedList(sortedList: MutableList<Int>, item: Int) {
        val index = sortedList.binarySearch(item)
        if (index >= 0) {
            sortedList.removeAt(index)
        }
    }
    
    fun median(values: List<Int>): Double {
        if (values.isEmpty()){
            return 0.0
        }
        return if (values.size % 2 == 0) {
            (values[values.size / 2-1] + values[values.size/2])/2.0
    
        } else {
            values[values.size/2].toDouble()
        }
    }
    
    fun activityNotifications(expenditure: Array<Int>, d: Int): Int {
        var numberOfNotifications = 0
        var startIndex = 0
        var valueToCheckIndex = d
        var sublist = expenditure.slice(startIndex..valueToCheckIndex-1).sorted().toMutableList()
        while (valueToCheckIndex < expenditure.size) {
            val m = median(sublist)
            val valueToCheck = expenditure[valueToCheckIndex]
            if (valueToCheck >= 2*m) {
                ++numberOfNotifications
            }
            removeItemFromSortedList(sublist, expenditure[startIndex])
            insertInSortedList(sublist, expenditure[valueToCheckIndex])
            ++startIndex
            ++valueToCheckIndex
        }
        return numberOfNotifications
    }