Created
May 8, 2021 00:09
-
-
Save germanescobar/89ec9c3ef69f50df4e2517d5665cfb35 to your computer and use it in GitHub Desktop.
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
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