Finding the Users Active Minutes — LeetCode 1817 Python Solution

MediumArrayHash Table
Problem
#1817
Pattern
Hash Map
Reading time
2 min

The problem

You are given the logs for users' actions on LeetCode, and an integer k. The logs are represented by a 2D integer array logs where each logs[i] = [IDi, timei] indicates that the user with IDi performed an action at the minute timei.

Example

Input
logs = [[0,5],[1,2],[0,2],[0,5],[1,3]], k = 5
Output
[0,2,0,0,0]
Explanation
The user with ID=0 performed actions at minutes 5, 2, and 5 again. Hence, they have a UAM of 2 (minute 5 is only counted once).

Python solution

Python
class Solution:
    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:
        d = defaultdict(set)
        for i, t in logs:
            d[i].add(t)
        ans = [0] * k
        for ts in d.values():
            ans[len(ts) - 1] += 1
        return ans

Complexity

MeasureComplexity
TimeO(n)
SpaceO(n), where n is the length of the logs array auxiliary

Pattern: Hash Map

Trade memory for time: remember what you have seen so the second pass never happens. LeetCode 1817. Finding the Users Active Minutes is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Hash Table.

The hash map guide has the Python template for the pattern and the 709 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1817. Finding the Users Active Minutes?
LeetCode 1817. Finding the Users Active Minutes is rated Medium on LeetCode.
What is the time complexity of LeetCode 1817. Finding the Users Active Minutes?
The Python solution on this page runs in O(n).
What is the space complexity of LeetCode 1817. Finding the Users Active Minutes?
The Python solution on this page uses O(n), where n is the length of the logs array auxiliary space.
What topics does LeetCode 1817. Finding the Users Active Minutes cover?
LeetCode 1817. Finding the Users Active Minutes is tagged Array and Hash Table on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview