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 a trailing number of days, they send the client a notification about potential fraud. The bank doesn't send the client any notifications until they have at least that trailing number of prior days' transaction data.
Given the number of trailing days 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.
For example, and . On the first three days, they just collect spending data. At day , we have trailing expenditures of . The median is and the day's expenditure is . Because , there will be a notice. The next day, our trailing expenditures are and the expenditures are . This is less than so no notice will be sent. Over the period, there was one notice sent.
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 values. (Wikipedia)
Function Description
Complete the function activityNotifications in the editor below. It must return an integer representing the number of client notifications.
activityNotifications has the following parameter(s):
- expenditure: an array of integers representing daily expenditures
- d: an integer, the lookback days for median spending
Input Format
The first line contains two space-separated integers and , the number of days of transaction data, and the number of trailing days' data used to calculate median spending.
The second line contains space-separated non-negative integers where each integer denotes .
Constraints
Output Format
Print an integer denoting the total number of times the client receives a notification over a period of days.
Sample Input 0
9 5
2 3 4 2 3 6 8 4 5
Sample Output 0
2
Explanation 0
We must determine the total number of the client receives over a period of days. For the first five days, the customer receives no notifications because the bank has insufficient transaction data: .
On the sixth day, the bank has days of prior transaction data, , and dollars. The client spends dollars, which triggers a notification because : .
On the seventh day, the bank has days of prior transaction data, , and dollars. The client spends dollars, which triggers a notification because : .
On the eighth day, the bank has days of prior transaction data, , and dollars. The client spends dollars, which does not trigger a notification because : .
On the ninth day, the bank has days of prior transaction data, , and a transaction median of dollars. The client spends dollars, which does not trigger a notification because : .
Sample Input 1
5 4
1 2 3 4 4
Sample Output 1
0
There are days of data required so the first day a notice might go out is day . Our trailing expenditures are with a median of The client spends which is less than so no notification is sent.
이 문제는 내용을 대충 읽어보면 뭐가 쉬운듯 하면서 뭔가 쉽게 풀수 있을 것 같은 느낌이 오는데......
몇 번 풀어보다보니 이거 원 정말 쉽지가 않다. 어렵다! ㅠ
저 문제에 있는 Median을 구하는 과정이 이해하기가 쉽지 않다.
어떨때 Notification 카운트를 올려야 하는지 ....
설명글에 있는 예제만 보면 뭔가 쉽게 설명이 되어 있는 듯 하나, 실제 테스트 케이스에 있는 문제들은 정말 정말 어렵다.
결국 혼자 해결하는 것을 포기하고 찾아보았다.
비스무리한 두 가지 예제를 찾았는데, 뭔가 코드에서 핵심 부분의 코드가 유사한 것으로 보아서 기본 원리가 뭔가가 있는 것 같다.
코드를 다시 몇번이고 꼼꼼히 따져보면서 코드 원리를 파악해봐야 겠다.
예제 #1
예제 #2
'Interview Preparation Kit > Sorting' 카테고리의 다른 글
Merge Sort: Counting Inversions (0) | 2018.08.07 |
---|---|
Sorting: Comparator (0) | 2018.07.25 |
Mark and Toys (0) | 2018.07.25 |
Bubble Sort (0) | 2018.07.25 |