Pankaj Tanwar
Published on

# Finding the Users Active Minutes.

––– views

Today marks day 30 of my coding diary. Let's directly, jump to the problem!

Problem of the day - Finding the Users Active Minutes

Tag - Medium

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 time.

Multiple users can perform actions simultaneously, and a single user can perform multiple actions in the same minute.

The user active minutes (UAM) for a given user is defined as the number of unique minutes in which the user performed an action on LeetCode. A minute can only be counted once, even if multiple actions occur during it.

You are to calculate a 1-indexed array answer of size k such that, for each j (1 <= j <= k), answer[j] is the number of users whose UAM equals j.

Return the array answer as described above.

Example 1:

Input: logs = [[0,5],[1,2],[0,2],[0,5],[1,3]], k = 5 Output: [0,2,0,0,0]

I don't like such long-problem-statement challenges. Mostly, these challenges just have a pretty simple algorithm. I think lengthy statements are just for scaring the hell out of me. Oops, I am a big boy now, let's try to solve it.

I decided to split the problem.

The first step would be to just find the unique logs from the logs array. So that, we can find the actions of each user.

To uniquize the array, my best friend, hashmap is always there to help.

Once I have the unique logs, I can find UAM for each user and find the result.

Here is the code, you can easily understand the algo behind it, it is quite simple.

class Solution {public:    vector<int> findingUsersActiveMinutes(vector<vector<int>>& logs, int k) {        map<pair<int,int>, int> list;        map<int,int> hash;                for(auto val: logs) {            list[make_pair(val[0],val[1])]++;            if(list[make_pair(val[0],val[1])] == 1) hash[val[0]]++;        }        vector<int> res(k, 0);        for(auto val: hash) {            cout << val.first << " " << val.second << endl;            if(val.second > 0) {                res[val.second-1]++;            }        }        return res;    }};

I just realized this is the ugliest code I could ever write. Two hashmaps, one vector, one pair, and some ugly C++ lines.

Although, It works perfectly for a given problem and passes all test cases. But, this is slow as hell.

I accepted Mission-Optimizer and started finding ways to optimize the code.

How can I forget my long-distance best friend, set. No one can deal with uniquization better than set.

Here is my beautiful modified code

class Solution {public:    vector<int> findingUsersActiveMinutes(vector<vector<int>>& logs, int k) {        unordered_map<int, unordered_set<int>> list;        for(auto val: logs) {            list[val[0]].insert(val[1]);        }                vector<int> res(k,0);                for(auto val: list) {            res[val.second.size() - 1]++;        }        return res;    }};

I have used, unordered_map & unordered_set, instead of map and set, to avoid extra runtime.

Ummm, that was a nice question. Although it was a long one still, I learned a new pattern of solving CP problems.

P.S. - Did you notice, I am no longer picking up easy problems.

I welcome your suggestions to improve it further!

Thanks for being part of my daily-code-workout journey. As always, if you have any thoughts about anything shared above, don't hesitate to reach out.

You might like previous editions of my coding diary