{"id":9640,"date":"2022-04-09T23:38:03","date_gmt":"2022-04-10T06:38:03","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9640"},"modified":"2022-04-09T23:38:40","modified_gmt":"2022-04-10T06:38:40","slug":"leetcode-2233-maximum-product-after-k-increments","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/priority-queue\/leetcode-2233-maximum-product-after-k-increments\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2233. Maximum Product After K Increments"},"content":{"rendered":"\n<p>You are given an array of non-negative integers&nbsp;<code>nums<\/code>&nbsp;and an integer&nbsp;<code>k<\/code>. In one operation, you may choose&nbsp;<strong>any<\/strong>&nbsp;element from&nbsp;<code>nums<\/code>&nbsp;and&nbsp;<strong>increment<\/strong>&nbsp;it by&nbsp;<code>1<\/code>.<\/p>\n\n\n\n<p>Return<em>&nbsp;the&nbsp;<strong>maximum<\/strong>&nbsp;<strong>product<\/strong>&nbsp;of&nbsp;<\/em><code>nums<\/code><em>&nbsp;after&nbsp;<strong>at most<\/strong>&nbsp;<\/em><code>k<\/code><em>&nbsp;operations.&nbsp;<\/em>Since the answer may be very large, return it&nbsp;<strong>modulo<\/strong>&nbsp;<code>10<sup>9<\/sup>&nbsp;+ 7<\/code>.<\/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 = [0,4], k = 5\n<strong>Output:<\/strong> 20\n<strong>Explanation:<\/strong> Increment the first number 5 times.\nNow nums = [5, 4], with a product of 5 * 4 = 20.\nIt can be shown that 20 is maximum product possible, so we return 20.\nNote that there may be other ways to increment nums to have the maximum product.\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 = [6,3,3,2], k = 2\n<strong>Output:<\/strong> 216\n<strong>Explanation:<\/strong> Increment the second number 1 time and increment the fourth number 1 time.\nNow nums = [6, 4, 3, 3], with a product of 6 * 4 * 3 * 3 = 216.\nIt can be shown that 216 is maximum product possible, so we return 216.\nNote that there may be other ways to increment nums to have the maximum product.\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, k &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>0 &lt;= nums[i] &lt;= 10<sup>6<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: priority queue<\/strong><\/h2>\n\n\n\n<p>Always increment the smallest number. Proof?<\/p>\n\n\n\n<p>Time complexity: O(klogn + nlogn)<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 maximumProduct(vector<int>& nums, int k) {\n    constexpr int kMod = 1e9 + 7;\n    priority_queue<int, vector<int>, greater<int>> q(begin(nums), end(nums));\n    while (k--) {\n      const int n = q.top(); \n      q.pop();\n      q.push(n + 1);\n    }\n    long long ans = 1;\n    while (!q.empty()) {\n      ans *= q.top(); q.pop();\n      ans %= kMod;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an array of non-negative integers&nbsp;nums&nbsp;and an integer&nbsp;k. In one operation, you may choose&nbsp;any&nbsp;element from&nbsp;nums&nbsp;and&nbsp;increment&nbsp;it by&nbsp;1. Return&nbsp;the&nbsp;maximum&nbsp;product&nbsp;of&nbsp;nums&nbsp;after&nbsp;at most&nbsp;k&nbsp;operations.&nbsp;Since the answer may be very&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[674],"tags":[88,177,72,338],"class_list":["post-9640","post","type-post","status-publish","format-standard","hentry","category-priority-queue","tag-greedy","tag-medium","tag-priority-queue","tag-product","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9640","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=9640"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9640\/revisions"}],"predecessor-version":[{"id":9643,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9640\/revisions\/9643"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}