Skip to content

Instantly share code, notes, and snippets.

@germanescobar
Created May 8, 2021 00:09
Show Gist options
  • Save germanescobar/89ec9c3ef69f50df4e2517d5665cfb35 to your computer and use it in GitHub Desktop.
Save germanescobar/89ec9c3ef69f50df4e2517d5665cfb35 to your computer and use it in GitHub Desktop.
var findingUsersActiveMinutes = function(logs, k) {
const users = groupActionsByUser(logs)
const uams = groupByActiveMinutes(users)
const result = []
for (let i=0; i < k; i++) {
result.push(uams[i+1] || 0)
}
return result
};
function groupActionsByUser(logs) {
const users = {}
for (log of logs) {
const [id, time] = log
if (users[id]) {
users[id].add(time)
} else {
users[id] = new Set([time])
}
}
return users
}
function groupByActiveMinutes(users) {
const uams = {}
for (id in users) {
const length = users[id].size
if (uams[length]) {
uams[length]++
} else {
uams[length] = 1
}
}
return uams
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment