-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
maximum-number-that-sum-of-the-prices-is-less-than-or-equal-to-k.py
131 lines (117 loc) · 3.98 KB
/
maximum-number-that-sum-of-the-prices-is-less-than-or-equal-to-k.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Time: O(max(logk, x) * log((logk) / x))
# Space: O((logk) / x)
# bit manipulation, binary search, combinatorics
class Solution(object):
def findMaximumNumber(self, k, x):
"""
:type k: int
:type x: int
:rtype: int
"""
def floor_log2(x):
return x.bit_length()-1
def binary_search_right(left, right, check):
while left <= right:
mid = left+(right-left)//2
if not check(mid):
right = mid-1
else:
left = mid+1
return right
def count(l):
return (prefix_cnt<<(x*l))+lookup[l]
result = prefix_cnt = 0
lookup = [0]
i = 0
while (lookup[-1]<<x)+(1<<(i+x-1)) <= k:
lookup.append((lookup[-1]<<x)+(1<<(i+x-1)))
i += x
while k >= prefix_cnt:
# l = result.bit_length()
# assert(prefix_cnt == sum(c == '1' and (l-i)%x == 0 for i, c in enumerate(bin(result)[2:])))
l = binary_search_right(1, len(lookup)-1, lambda l: count(l) <= k)
cnt, i = count(l), x*l
c = min(floor_log2(k//cnt) if cnt else float("inf"), x-1)
cnt <<= c
i += c
k -= cnt
result += 1<<i
prefix_cnt += int((i+1)%x == 0)
return result-1
# Time: O(max(logk, x) * (max(logk, x) / x))
# Space: O(1)
# bit manipulation, combinatorics
class Solution2(object):
def findMaximumNumber(self, k, x):
"""
:type k: int
:type x: int
:rtype: int
"""
def floor_log2(x):
return x.bit_length()-1
result = prefix_cnt = 0
while k >= prefix_cnt:
# l = result.bit_length()
# assert(prefix_cnt == sum(c == '1' and (l-i)%x == 0 for i, c in enumerate(bin(result)[2:])))
cnt, i = prefix_cnt, 0
while (cnt<<x)+(1<<(i+x-1)) <= k:
cnt = (cnt<<x)+(1<<(i+x-1))
i += x
c = min(floor_log2(k//cnt) if cnt else float("inf"), x-1)
cnt <<= c
i += c
k -= cnt
result += 1<<i
prefix_cnt += int((i+1)%x == 0)
return result-1
# Time: O(max(logk, x)^2)
# Space: O(1)
# bit manipulation, combinatorics
class Solution3(object):
def findMaximumNumber(self, k, x):
"""
:type k: int
:type x: int
:rtype: int
"""
def floor_log2(x):
return x.bit_length()-1
result = prefix_cnt = 0
while k >= prefix_cnt:
# l = result.bit_length()
# assert(prefix_cnt == sum(c == '1' and (l-i)%x == 0 for i, c in enumerate(bin(result)[2:])))
cnt, i = prefix_cnt, 0
while (cnt<<1)+(1<<i if (i+1)%x == 0 else 0) <= k:
cnt = (cnt<<1)+(1<<i if (i+1)%x == 0 else 0)
i += 1
k -= cnt
result += 1<<i
prefix_cnt += int((i+1)%x == 0)
return result-1
# Time: O(max(logk, x) * (max(logk, x) / x))
# Space: O(1)
# bit manipulation, binary search, combinatorics
class Solution4(object):
def findMaximumNumber(self, k, x):
"""
:type k: int
:type x: int
:rtype: int
"""
def binary_search_right(left, right, check):
while left <= right:
mid = left+(right-left)//2
if not check(mid):
right = mid-1
else:
left = mid+1
return right
def count(v):
cnt = i = 0
while 1<<(i+x-1) <= v:
q, r = divmod(v+1, 1<<((i+x-1)+1))
cnt += q*1*(1<<(i+x-1))+max(r-1*(1<<(i+x-1)), 0)
i += x
return cnt
return binary_search_right(1, max(k<<2, 1<<x), lambda v: count(v) <= k) # right bound is verified by checking all possible (k, v) values, or just set right = solution.findMaximumNumber(10**15, 8) <= 10**15