forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximum-product-after-k-increments.py
70 lines (61 loc) · 1.66 KB
/
maximum-product-after-k-increments.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Time: O(nlogn)
# Space: O(1)
# math, sort
class Solution(object):
def maximumProduct(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
MOD = 10**9+7
nums.sort()
total = sum(nums)
for i in reversed(xrange(len(nums))):
if nums[i]*(i+1)-total <= k:
break
total -= nums[i]
q, r = divmod(k+total, i+1)
return (pow(q, (i+1)-r, MOD)*pow(q+1, r, MOD)*
reduce(lambda x, y: x*y%MOD, (nums[j] for j in xrange(i+1, len(nums))), 1)) % MOD
# Time: O(n + k)
# Space: O(n)
import collections
# freq table
class Solution2(object):
def maximumProduct(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
MOD = 10**9+7
cnt = collections.Counter(nums)
min_num = min(cnt.iterkeys())
while k:
c = min(cnt[min_num], k)
cnt[min_num] -= c
cnt[min_num+1] += c
if not cnt[min_num]:
del cnt[min_num]
min_num += 1
k -= c
return reduce(lambda total, x: total*pow(x[0], x[1], MOD)%MOD, cnt.iteritems(), 1)
# Time: O(n + klogn)
# Space: O(1)
import heapq
# heap
class Solution3(object):
def maximumProduct(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
MOD = 10**9+7
min_heap = nums
heapq.heapify(min_heap)
while k:
heapq.heappush(min_heap, heapq.heappop(min_heap)+1)
k -= 1
return reduce(lambda x, y: x*y%MOD, min_heap)