Advertisement
kosievdmerwe

Untitled

Nov 16th, 2021
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.65 KB | None | 0 0
  1. class Solution:
  2.     def findKthNumber(self, m: int, n: int, k: int) -> int:
  3.         # Make m >= n
  4.         if m < n:
  5.             m, n = n, m
  6.            
  7.         @cache
  8.         def num_smaller_diag(x: int) -> int:
  9.             y = min(x, n)
  10.             target = x * y
  11.             ans = 1 if x == y else 0
  12.             for cy in range(1, y + 1):
  13.                 cx = target // cy
  14.                 if target % cy == 0:
  15.                     cx -= 1
  16.                 cx = min(cx, m)
  17.                
  18.                 ans += cx - cy + 1
  19.                 ans += min(cx, n) - cy
  20.                
  21.             return ans
  22.    
  23.         # def num_smaller_diag_slow(x: int) -> int:
  24.         #     y = min(x, n)
  25.         #     target = x * y
  26.         #     ans = 0
  27.         #     for cx in range(1, m + 1):
  28.         #         for cy in range(1, n + 1):
  29.         #             if cx * cy < target:
  30.         #                 ans += 1
  31.         #     return ans
  32.         # for x in range(1, m + 1):
  33.         #     if num_smaller_diag(x) != num_smaller_diag_slow(x):
  34.         #         print(x, num_smaller_diag(x), num_smaller_diag_slow(x))
  35.            
  36.         b, e = 1, m + 1
  37.         smaller_x = 1
  38.         while b < e:
  39.             tx = (b + e) // 2
  40.             num_smaller = num_smaller_diag(tx)
  41.             if num_smaller < k:
  42.                 smaller_x = tx
  43.                 b = tx + 1
  44.             else:
  45.                 e = tx
  46.                
  47.         num_smaller = num_smaller_diag(smaller_x)
  48.         smaller_target = smaller_x * min(smaller_x, n)
  49.         next_target = (smaller_target + 1) * min(smaller_x + 1, n)
  50.         if num_smaller + 1 == k:
  51.             return smaller_target
  52.        
  53.        
  54.        
  55.         nums = []
  56.         for y in range(1, min(smaller_x, n) + 1):
  57.             min_x, max_x = (smaller_target + y - 1) // y, next_target // y
  58.             if min_x > m:
  59.                 continue
  60.             if next_target % y == 0:
  61.                 max_x -= 1
  62.             min_x = max(min_x, y)
  63.             max_x = min(max_x, m)
  64.             for x in range(min_x, max_x + 1):
  65.                 nums.append(x * y)
  66.                 if x != y and x <= n:
  67.                     nums.append(x * y)
  68.         nums.sort()
  69.        
  70.         # test_nums = []
  71.         # for x in range(1, m + 1):
  72.         #     for y in range(1, n + 1):
  73.         #         if smaller_target <= x * y < next_target:
  74.         #             test_nums.append(x * y)
  75.         # test_nums.sort()
  76.         # print(nums == test_nums, smaller_target, next_target)
  77.         # print(nums)
  78.         # print(test_nums)
  79.         # print()
  80.        
  81.         return nums[k - num_smaller - 1]
  82.            
  83.        
  84.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement