-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
minimum-number-of-coins-to-be-added.py
53 lines (50 loc) · 1.31 KB
/
minimum-number-of-coins-to-be-added.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
# Time: O(nlogn + logt)
# Space: O(1)
# lc0330
# sort, greedy
class Solution(object):
def minimumAddedCoins(self, coins, target):
"""
:type coins: List[int]
:type target: int
:rtype: int
"""
coins.sort()
result = reachable = 0
for x in coins:
# if x > target:
# break
while not reachable >= x-1:
result += 1
reachable += reachable+1
reachable += x
while not reachable >= target:
result += 1
reachable += reachable+1
return result
# Time: O(nlogn + logt)
# Space: O(1)
# lc0330
# sort, greedy
class Solution2(object):
def minimumAddedCoins(self, coins, target):
"""
:type coins: List[int]
:type target: int
:rtype: int
"""
coins.sort()
result = reachable = 0
for x in coins:
while not reachable >= x-1:
result += 1
reachable += reachable+1
if reachable >= target:
return result
reachable += x
if reachable >= target:
return result
while not reachable >= target:
result += 1
reachable += reachable+1
return result