{"id":9567,"date":"2022-03-11T21:14:32","date_gmt":"2022-03-12T05:14:32","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9567"},"modified":"2022-03-11T21:15:32","modified_gmt":"2022-03-12T05:15:32","slug":"leetcode-2190-most-frequent-number-following-key-in-an-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-2190-most-frequent-number-following-key-in-an-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2190. Most Frequent Number Following Key In an Array"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>nums<\/code>.<strong>&nbsp;<\/strong>You are also given an integer&nbsp;<code>key<\/code>, which is present in&nbsp;<code>nums<\/code>.<\/p>\n\n\n\n<p>For every unique integer&nbsp;<code>target<\/code>&nbsp;in&nbsp;<code>nums<\/code>,&nbsp;<strong>count<\/strong>&nbsp;the number of times&nbsp;<code>target<\/code>&nbsp;immediately follows an occurrence of&nbsp;<code>key<\/code>&nbsp;in&nbsp;<code>nums<\/code>. In other words, count the number of indices&nbsp;<code>i<\/code>&nbsp;such that:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>0 &lt;= i &lt;= nums.length - 2<\/code>,<\/li><li><code>nums[i] == key<\/code>&nbsp;and,<\/li><li><code>nums[i + 1] == target<\/code>.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<\/em><code>target<\/code><em>&nbsp;with the&nbsp;<strong>maximum<\/strong>&nbsp;count<\/em>. The test cases will be generated such that the&nbsp;<code>target<\/code>&nbsp;with maximum count is unique.<\/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> nums = [1,100,200,1,100], key = 1\n<strong>Output:<\/strong> 100\n<strong>Explanation:<\/strong> For target = 100, there are 2 occurrences at indices 1 and 4 which follow an occurrence of key.\nNo other integers follow an occurrence of key, so we return 100.\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> nums = [2,2,2,2,3], key = 2\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> For target = 2, there are 3 occurrences at indices 1, 2, and 3 which follow an occurrence of key.\nFor target = 3, there is only one occurrence at index 4 which follows an occurrence of key.\ntarget = 2 has the maximum number of occurrences following an occurrence of key, so we return 2.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= nums.length &lt;= 1000<\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 1000<\/code><\/li><li>The test cases will be generated such that the answer is unique.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int mostFrequent(vector<int>& nums, int key) {\n    const int n = nums.size();\n    unordered_map<int, int> m;\n    int count = 0;\n    int ans = 0;\n    for (int i = 1; i < n; ++i) {\n      if (nums[i - 1] == key &#038;&#038; ++m[nums[i]] > count) {\n        count = m[nums[i]];\n        ans = nums[i];\n      }\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;integer array&nbsp;nums.&nbsp;You are also given an integer&nbsp;key, which is present in&nbsp;nums. For every unique integer&nbsp;target&nbsp;in&nbsp;nums,&nbsp;count&nbsp;the number of times&nbsp;target&nbsp;immediately follows an occurrence of&nbsp;key&nbsp;in&nbsp;nums.&#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":[222,82],"class_list":["post-9567","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-easy","tag-hashtable","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9567","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=9567"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9567\/revisions"}],"predecessor-version":[{"id":9569,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9567\/revisions\/9569"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}