Skip to content

Instantly share code, notes, and snippets.

@1ou
Created May 12, 2021 12:02
Show Gist options
  • Save 1ou/475b263a47ab43d803cbde16df07fdb6 to your computer and use it in GitHub Desktop.
Save 1ou/475b263a47ab43d803cbde16df07fdb6 to your computer and use it in GitHub Desktop.
yandex
/*
Есть последовательность запросов пользователей, каждый запрос —
это пара (time, userId), запросы всегда приходят в отсортированном
по времени порядке.
Нужно уметь быстро отвечать: "сколько за последние k секунд было
пользователей, которые задали >= limit запросов".
*/
class UserStatistics {
Map<Long, List<Long>> usersFrequencies;
private int k;
private int limit;
private int lastTime;
private [] = new long[k]
public UserStatistics(int k, int limit) {
this.k = k;
this.limit = limit;
}
public void event(long time, long userId) {
if (usersFrequencies.contains(userId)) {
usersFrequencies.get(userId).add(time);
} else {
List<Long> list = new ArrayList<>();
usersFrequencies.put(userId, list);
list.add(time);
}
long diff = time - k;
lastTime = time;
}
public int robotCount(long time) {
long diff = time - k;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment