serega1112

668 - binary search

Dec 21st, 2020 (edited)
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. class Solution:
  2.     def findKthNumber(self, m: int, n: int, k: int) -> int:
  3.        
  4.         l = 0
  5.         r = m * n
  6.  
  7.         def check(x):
  8.             i = n
  9.             j = 1
  10.             count = 0
  11.             while i > 0 and j <= m:
  12.                 if i * j <= x:
  13.                     count += i
  14.                     j += 1
  15.                 else:
  16.                     i -= 1
  17.             return count
  18.  
  19.  
  20.         while r - l > 1:
  21.             mid = l + (r - l) // 2
  22.             if check(mid) < k:
  23.                 l = mid
  24.             else:
  25.                 r = mid
  26.  
  27.         return r
Add Comment
Please, Sign In to add comment