{"id":8388,"date":"2021-04-25T09:28:49","date_gmt":"2021-04-25T16:28:49","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8388"},"modified":"2021-04-25T11:42:25","modified_gmt":"2021-04-25T18:42:25","slug":"leetcode-1838-frequency-of-the-most-frequent-element","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/sliding-window\/leetcode-1838-frequency-of-the-most-frequent-element\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1838. Frequency of the Most Frequent Element"},"content":{"rendered":"\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1838. Frequency of the Most Frequent Element - \u5237\u9898\u627e\u5de5\u4f5c EP392\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/KhQOPSa09Fs?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>The&nbsp;<strong>frequency<\/strong>&nbsp;of an element is the number of times it occurs in an array.<\/p>\n\n\n\n<p>You are given an integer array&nbsp;<code>nums<\/code>&nbsp;and an integer&nbsp;<code>k<\/code>. In one operation, you can choose an index of&nbsp;<code>nums<\/code>&nbsp;and increment the element at that index by&nbsp;<code>1<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>maximum possible frequency<\/strong>&nbsp;of an element after performing&nbsp;<strong>at most<\/strong>&nbsp;<\/em><code>k<\/code><em>&nbsp;operations<\/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> nums = [1,2,4], k = 5\n<strong>Output:<\/strong> 3<strong>\nExplanation:<\/strong> Increment the first element three times and the second element two times to make nums = [4,4,4].\n4 has a frequency of 3.<\/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 = [1,4,8,13], k = 5\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> There are multiple optimal solutions:\n- Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2.\n- Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2.\n- Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [3,9,6], k = 2\n<strong>Output:<\/strong> 1\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= k &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Sliding Window<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1838-ep392-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1838-ep392-1.png\" alt=\"\" class=\"wp-image-8391\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1838-ep392-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1838-ep392-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1838-ep392-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1838-ep392-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1838-ep392-2.png\" alt=\"\" class=\"wp-image-8393\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1838-ep392-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1838-ep392-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1838-ep392-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1838-ep392-3.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1838-ep392-3.png\" alt=\"\" class=\"wp-image-8394\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1838-ep392-3.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1838-ep392-3-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1838-ep392-3-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<p>Sort the elements, maintain a window such that it takes at most k ops to make the all the elements equal to nums[i].<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(1)<\/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  int maxFrequency(vector<int>& nums, int k) {\n    sort(begin(nums), end(nums));\n    int l = 0;\n    long sum = 0;\n    int ans = 0;\n    for (int r = 0; r < nums.size(); ++r) {\n      sum += nums[r];      \n      while (l < r &#038;&#038; \n             sum + k < static_cast<long>(nums[r]) * (r - l + 1))\n        sum -= nums[l++];\n      ans = max(ans, r - l + 1);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The&nbsp;frequency&nbsp;of an element is the number of times it occurs in an array. You are given an integer array&nbsp;nums&nbsp;and an integer&nbsp;k. In one operation, you&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[476],"tags":[177,215,23],"class_list":["post-8388","post","type-post","status-publish","format-standard","hentry","category-sliding-window","tag-medium","tag-sliding-window","tag-sort","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8388","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=8388"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8388\/revisions"}],"predecessor-version":[{"id":8397,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8388\/revisions\/8397"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}