Created
May 12, 2021 12:02
-
-
Save 1ou/475b263a47ab43d803cbde16df07fdb6 to your computer and use it in GitHub Desktop.
yandex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Есть последовательность запросов пользователей, каждый запрос — | |
это пара (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