Skip to content

Instantly share code, notes, and snippets.

@namthatman
Created June 23, 2019 06:42
Show Gist options
  • Save namthatman/254dac29edeac8df39d285ee988190c3 to your computer and use it in GitHub Desktop.
Save namthatman/254dac29edeac8df39d285ee988190c3 to your computer and use it in GitHub Desktop.
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* readline();
char** split_string(char*);
int compare(const void* a, const void* b) {
return (*(int*)a - *(int*)b);
}
float getMedian(int* arr, int d, int index) {
if (d % 2 == 0)
return (float)(arr[index] + arr[index+1]) / 2;
else
return (float)arr[index+1];
}
// Complete the activityNotifications function below.
int activityNotifications(int expenditure_count, int* expenditure, int d) {
int notify = 0;
int* arr = malloc(d * sizeof(int));
int median_index = d/2 - 1;
for (int i=d; i < expenditure_count; i++) {
int id = i;
for (int j=d-1; j >= 0; j--) {
arr[j] = expenditure[--id];
}
qsort(arr, d, sizeof(int), compare);
float median = getMedian(arr, d, median_index);
if (expenditure[i] >= 2 * median) {
notify++;
}
}
return notify;
}
int main()
{
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
char** nd = split_string(readline());
char* n_endptr;
char* n_str = nd[0];
int n = strtol(n_str, &n_endptr, 10);
if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); }
char* d_endptr;
char* d_str = nd[1];
int d = strtol(d_str, &d_endptr, 10);
if (d_endptr == d_str || *d_endptr != '\0') { exit(EXIT_FAILURE); }
char** expenditure_temp = split_string(readline());
int* expenditure = malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
char* expenditure_item_endptr;
char* expenditure_item_str = *(expenditure_temp + i);
int expenditure_item = strtol(expenditure_item_str, &expenditure_item_endptr, 10);
if (expenditure_item_endptr == expenditure_item_str || *expenditure_item_endptr != '\0') { exit(EXIT_FAILURE); }
*(expenditure + i) = expenditure_item;
}
int expenditure_count = n;
int result = activityNotifications(expenditure_count, expenditure, d);
fprintf(fptr, "%d\n", result);
fclose(fptr);
return 0;
}
char* readline() {
size_t alloc_length = 1024;
size_t data_length = 0;
char* data = malloc(alloc_length);
while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin);
if (!line) { break; }
data_length += strlen(cursor);
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; }
size_t new_length = alloc_length << 1;
data = realloc(data, new_length);
if (!data) { break; }
alloc_length = new_length;
}
if (data[data_length - 1] == '\n') {
data[data_length - 1] = '\0';
}
data = realloc(data, data_length);
return data;
}
char** split_string(char* str) {
char** splits = NULL;
char* token = strtok(str, " ");
int spaces = 0;
while (token) {
splits = realloc(splits, sizeof(char*) * ++spaces);
if (!splits) {
return splits;
}
splits[spaces - 1] = token;
token = strtok(NULL, " ");
}
return splits;
}
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 2x 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 d and a client's total daily expenditures for a period of n days, find and print the number of times the client will receive a notification over all n days.
For example, d=3 and expenditures = [10,20,30,40,50]. On the first three days, they just collect spending data. At day 4, we have trailing expenditures of [10,20,30]. The median is 20 and the day's expenditure is 40. Because 40 >= 2x20, there will be a notice. The next day, our trailing expenditures are [20,30,40] and the expenditures are 50. This is less than 2x30 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
@TFang96
Copy link

TFang96 commented Jul 27, 2023

Does this execute within the time limit? Looks like the algorithm I had but it fails due to time constraints.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment