Python Binary Search


  • 0
    class Solution(object):
        def findKthNumber(self, m, n, k):
            def binary(num):
                cnt = 0
                for i in range(1, m + 1):
                    cnt += min(n, num / i)
                return cnt
            left, right = 0, m * n + 1
            while right - left > 1:
                mid = (left + right) >> 1
                if binary(mid) < k:
                    left = mid
                else:
                    right = mid
            return left + 1
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.