1 minute read

Fraudulent Activity Notifications (Hacker Rank)

HackerLand National Bank has a simple policy for warning clients about possible fraudulent account activity. If the amount spent by a client on a particular day is greater than or equal to the client’s median spending for the last days, they send the client a notification about potential fraud. The bank doesn’t send the client any notifications until they have at least prior days of transaction data.

Given the value of and a client’s total daily expenditures for a period of days, find and print the number of times the client will receive a notification over all days.

Note: The median of a list of numbers can be found by arranging all the numbers from smallest to greatest. If there is an odd number of numbers, the middle one is picked. If there is an even number of numbers, median is then defined to be the average of the two middle

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
    public static int activityNotifications(int[] expenditure, int d){
        int[] count = new int[201];
        int result = 0;
        for(int i = 0; i < d; i++){
            count[expenditure[i]]++;
        }
        for(int i = d; i < expenditure.length; i++){
            int median = getMedian(count, d);
            if(median <= expenditure[i]){
                result++;
            }
            count[expenditure[i-d]]--;
            count[expenditure[i]]++;
        }
        return result;
    }
    public static int getMedian(int[] count, int d){
        int sum = 0;
        for(int i = 0; i < count.length; i++){
            sum += count[i];
            if((2*sum) == d){
                return (2*i+1);
            }else if((2*sum) > d){
                return (i*2);
            }
        }
        return 1;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int d = in.nextInt();
        int[] expenditure = new int[n];
        for(int i = 0; i < n; i++){
            expenditure[i] = in.nextInt();
        }
        int result = activityNotifications(expenditure, d);
        System.out.println(result);
        in.close();
    }
}

행운의 77문제 프로젝트
  행운의 77문제 프로젝트는 한 달동안 알고리즘 문제 77개를 푸는 프로젝트입니다.