Skip to content

Commit cd3927a

Browse files
authored
Update maximum-product-after-k-increments.py
1 parent d4b2c32 commit cd3927a

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

Python/maximum-product-after-k-increments.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ def maximumProduct(self, nums, k):
3838
cnt = collections.Counter(nums)
3939
min_num = min(cnt.iterkeys())
4040
while k:
41-
cnt[min_num] -= 1
42-
cnt[min_num+1] += 1
41+
c = min(cnt[min_num], k)
42+
cnt[min_num] -= c
43+
cnt[min_num+1] += c
4344
if not cnt[min_num]:
4445
del cnt[min_num]
4546
min_num += 1
46-
k -= 1
47+
k -= c
4748
return reduce(lambda total, x: total*pow(x[0], x[1], MOD)%MOD, cnt.iteritems(), 1)
4849

4950

0 commit comments

Comments
 (0)