{"id":8323,"date":"2021-04-06T17:05:02","date_gmt":"2021-04-07T00:05:02","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8323"},"modified":"2021-04-06T23:08:05","modified_gmt":"2021-04-07T06:08:05","slug":"leetcode-1817-finding-the-users-active-minutes","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1817-finding-the-users-active-minutes\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1817. Finding the Users Active Minutes"},"content":{"rendered":"\n<p>You are given the logs for users&#8217; actions on LeetCode, and an integer&nbsp;<code>k<\/code>. The logs are represented by a 2D integer array&nbsp;<code>logs<\/code>&nbsp;where each&nbsp;<code>logs[i] = [ID<sub>i<\/sub>, time<sub>i<\/sub>]<\/code>&nbsp;indicates that the user with&nbsp;<code>ID<sub>i<\/sub><\/code>&nbsp;performed an action at the minute&nbsp;<code>time<sub>i<\/sub><\/code>.<\/p>\n\n\n\n<p><strong>Multiple users<\/strong>&nbsp;can perform actions simultaneously, and a single user can perform&nbsp;<strong>multiple actions<\/strong>&nbsp;in the same minute.<\/p>\n\n\n\n<p>The&nbsp;<strong>user active minutes (UAM)<\/strong>&nbsp;for a given user is defined as the&nbsp;<strong>number of unique minutes<\/strong>&nbsp;in which the user performed an action on LeetCode. A minute can only be counted once, even if multiple actions occur during it.<\/p>\n\n\n\n<p>You are to calculate a&nbsp;<strong>1-indexed<\/strong>&nbsp;array&nbsp;<code>answer<\/code>&nbsp;of size&nbsp;<code>k<\/code>&nbsp;such that, for each&nbsp;<code>j<\/code>&nbsp;(<code>1 &lt;= j &lt;= k<\/code>),&nbsp;<code>answer[j]<\/code>&nbsp;is the&nbsp;<strong>number of users<\/strong>&nbsp;whose&nbsp;<strong>UAM<\/strong>&nbsp;equals&nbsp;<code>j<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the array&nbsp;<\/em><code>answer<\/code><em>&nbsp;as described above<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> logs = [[0,5],[1,2],[0,2],[0,5],[1,3]], k = 5\n<strong>Output:<\/strong> [0,2,0,0,0]\n<strong>Explanation:<\/strong>\nThe 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).\nThe user with ID=1 performed actions at minutes 2 and 3. Hence, they have a UAM of 2.\nSince both users have a UAM of 2, answer[2] is 2, and the remaining answer[j] values are 0.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> logs = [[1,1],[2,2],[2,3]], k = 4\n<strong>Output:<\/strong> [1,1,0,0]\n<strong>Explanation:<\/strong>\nThe user with ID=1 performed a single action at minute 1. Hence, they have a UAM of 1.\nThe user with ID=2 performed actions at minutes 2 and 3. Hence, they have a UAM of 2.\nThere is one user with a UAM of 1 and one with a UAM of 2.\nHence, answer[1] = 1, answer[2] = 1, and the remaining values are 0.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= logs.length &lt;= 10<sup>4<\/sup><\/code><\/li><li><code>0 &lt;= ID<sub>i<\/sub>&nbsp;&lt;= 10<sup>9<\/sup><\/code><\/li><li><code>1 &lt;= time<sub>i<\/sub>&nbsp;&lt;= 10<sup>5<\/sup><\/code><\/li><li><code>k<\/code>&nbsp;is in the range&nbsp;<code>[The maximum&nbsp;<strong>UAM<\/strong>&nbsp;for a user, 10<sup>5<\/sup>]<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashsets in a Hashtable<\/strong><\/h2>\n\n\n\n<p>key: user_id, value: set{time} <\/p>\n\n\n\n<p>Time complexity: O(n + k)<br>Space complexity: O(n + k)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  vector<int> findingUsersActiveMinutes(vector<vector<int>>& logs, int k) {\n    unordered_map<int, unordered_set<int>> m;\n    vector<int> ans(k);\n    for (const auto& log : logs)\n      m[log[0]].insert(log[1]);\n    for (const auto& [id, s] : m)\n      ++ans[s.size() - 1];\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You are given the logs for users&#8217; actions on LeetCode, and an integer&nbsp;k. The logs are represented by a 2D integer array&nbsp;logs&nbsp;where each&nbsp;logs[i] = [IDi,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[8,299,82,177],"class_list":["post-8323","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-counting","tag-hashset","tag-hashtable","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8323"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8323\/revisions"}],"predecessor-version":[{"id":8333,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8323\/revisions\/8333"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}