Skip to content
LC-0793 Hard LeetCode

793. Preimage Size of Factorial Zeroes Function

Read the full problem statement on LeetCode.
Difficulty: hard Acceptance: 46% Topics: Math, Binary Search
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O((logn)^2)
# Space: O(1)

class Solution(object):
    def preimageSizeFZF(self, K):
        """
        :type K: int
        :rtype: int
        """
        def count_of_factorial_primes(n, p):
            cnt = 0
            while n > 0:
                cnt += n//p
                n //= p
            return cnt

        p = 5
        left, right = 0, p*K
        while left <= right:
            mid = left + (right-left)//2
            if count_of_factorial_primes(mid, p) >= K:
                right = mid-1
            else:
                left = mid+1
        return p if count_of_factorial_primes(left, p) == K else 0

Solution from kamyu104/LeetCode-Solutions · MIT