题目

解法1

首先理解一下题意,要求的是对数组中某些数字增加1后的最大乘积,一共可增加k次。对于每一次增加1,整个乘积的变化是除了被增加这个数字以外的数字总和。那么最后乘积最大也就是k次增加的最多。从贪心的思想来看,保证每次增加都是最大即可,而每次增加要最大,也就是被增加的数字是所有数字中最小的即可。
自然而然地想到用min heap来保证能够快速找到最小的数字
note:这道题目有个点很有意思,如果这个mod放在最后进行,那么会TLE,只有放在while循环内部才能过,理论上时间复杂度是一模一样的

class Solution {public:int maximumProduct(vector<int>& nums, int k) {int tmp = pow(10,9) + 7;priority_queue<int,vector<int>,greater<int>> pq;for(auto& num : nums){pq.push(num);}while(k > 0){int curr = pq.top();pq.pop();pq.push(++curr);k--;}long long res = 1;while(!pq.empty()){int top = pq.top();pq.pop();res = (res * top) % tmp;}return res;}
};

时间复杂度:(Onlogn),n为数组长度
空间复杂度:O(n)

解法2

在这边评论区看到个非常有意思的答案.https://leetcode.com/problems/maximum-product-after-k-increments/discuss/1930994/Easy-Python-Solution-with-Explanation
首先数出所有数字的个数,从最小的数字开始,如果当前数字个数变0,那么下一个操作的数字变成当前的数字加一。这种解法的巧妙之处在于,当当前数字个数变为0的时候,这个数字加1必存在,也必定是当前的最小数了。这样成功将时间复杂度变为O(n)

class Solution:def maximumProduct(self, nums: List[int], k: int) -> int:counts = collections.Counter(nums)# print(counts)num = min(counts.keys())  # start from minimum numberwhile k > 0:counts[num] -= 1 counts[num+1] += 1if counts[num] == 0:  # if there are no more elements in this "bucket" or number, then we go to the next highest numnum += 1k -= 1product = 1for num, count in counts.items():if count > 0:product *= (num**count)return product % (10**9 + 7)

Leetcode 2233. Maximum Product After K Increments相关推荐

  1. LeetCode 152. Maximum Product Subarray--动态规划--C++,Python解法

    题目地址:Maximum Product Subarray - LeetCode Given an integer array nums, find the contiguous subarray w ...

  2. LeetCode 628. Maximum Product of Three Numbers

    题目: Given an integer array, find three numbers whose product is maximum and output the maximum produ ...

  3. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

  4. LeetCode 152. Maximum Product Subarray

    152. Maximum Product Subarray Find the contiguous subarray within an array (containing at least one ...

  5. leetcode 318. Maximum Product of Word Lengths | 318. 最大单词长度乘积

    题目 https://leetcode.com/problems/maximum-product-of-word-lengths/ 题解 中规中矩的题目,中规中矩的思路,不像是一个 medium 题. ...

  6. leetcode 1339. Maximum Product of Splitted Binary Tree | 1339. 分裂二叉树的最大乘积(树形dp)

    题目 https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/ 题解 树形 dp,思路见草稿 /*** Defini ...

  7. leetcode 152. Maximum Product Subarray | 152. 乘积最大子数组(Java)

    题目 https://leetcode.com/problems/maximum-product-subarray/ 题解 class Solution {public int maxProduct( ...

  8. leetcode 152. Maximum Product Subarry

    这道题求的是乘积的最大值的,那么依照之前的和的最大值类似的做法的,乘积的最大值可能是在最大值*当前值和最小值*当前值和当前值三者之间取得的最大值的,那么使用两个变量来保存每一步的最大最小值的. cla ...

  9. leetcode 628. Maximum Product of Three Numbers | 628. 三个数的最大乘积(Java)

    题目 https://leetcode-cn.com/problems/maximum-product-of-three-numbers/ 题解 找到三个数的最大乘积.先给数组排序,考虑可能有负数的情 ...

最新文章

  1. [BZOJ4811][Ynoi2017]由乃的OJ 树链剖分+线段树
  2. [私]-图片backup
  3. C语言 第五章 循环结构
  4. 职校学计算机对口高考可以考幼师吗,幼师专业对口高考考那些
  5. Linux 虚拟机配置-network is unreachable
  6. vue 多点触控手势_手写 Vue 手势组件__Vue.js
  7. thinkphp上传
  8. windows10未安装任何音频输出设备(1903、1909)
  9. html怎么消除打印的进纸,关于打印机进纸故障的原因分析和解决方法(一)
  10. 计算机共享wf,电脑怎么共享wifi网络
  11. UML--用例图详解
  12. 浅谈FPGA,SoC,ASIC
  13. Unity LineRenderer 画运动轨迹
  14. 微信android返回上一页位置,解决微信内置浏览器返回上一页强制刷新问题方法...
  15. git更换远程仓库和用户名密码
  16. AcWing 342. 道路与航线 (双端队列广搜问题,SPFA)
  17. 浪潮官网服务器型号,浪潮服务器有哪些型号,哪位了解?
  18. 《三体》里的超级计算机,我们今天能造出来吗?
  19. Cordova打包Scratch为APP
  20. paper:DeepAR: Probabilistic forecasting with autoregressive recurrent networks DeepAR模型

热门文章

  1. x在计算机中是哪个按键,电脑键盘x号怎么打出来
  2. 优雅地实现一个高效、异步数据实时刷新的列表
  3. python下的xml创建以及追加信息,删除信息方法
  4. 一时语噻:二面鹅厂,面试官问出Nginx你了解吗?
  5. java pdf转图片原理_pdf转图片程序(java实现)
  6. 推荐系统之用户标签,以及基于标签的算法
  7. VScode恢复删除的文件
  8. sql语句查询一天24小时每个小时数据,查询时间段内每天数据
  9. Boot(重点SCSS☆☆☆☆☆)(day03)
  10. CCF关于举办CSP-J1 CSP-S1 初赛的报名通知