[
binary search
]
BinarySearch 1029 Log Truncation
Problem statement
https://binarysearch.com/problems/Log-Truncation/
Solution
Just use binary search here, where check(x)
is possibility to do logs.
Complexity
It is O(n log M)
for time and O(n)
for space, where M = max(A)
.
Code
class Solution:
def solve(self, A, limit):
def check(x):
return sum(min(x, t) for t in A) <= limit
beg, end = -1, max(A) + 1
while beg + 1 < end:
mid = (beg + end)//2
if check(mid):
beg = mid
else:
end = mid
return beg