nikunjsoni

668

May 20th, 2021
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int findKthNumber(int m, int n, int k) {
  4.         int rows = m, cols = n, l=1, r = m*n;
  5.        
  6.         // Binary search on answer space.
  7.         int ans = 0;
  8.         while(l<r){
  9.             int mid = (l+r)/2;
  10.             int count = getCount(mid, rows, cols);
  11.             if(count < k)
  12.                 l = mid+1;
  13.             else
  14.                 r = mid;
  15.         }
  16.         return l;
  17.     }
  18.    
  19.     int getCount(int k, int rows, int cols){
  20.         int count = 0, curX = rows-1, curY = 0;
  21.         // Count numbers less-equal k.
  22.         for(int i=1; i<=rows; i++)
  23.             count += min(k/i, cols);
  24.         return count;
  25.     }
  26.    
  27. };
Advertisement
Add Comment
Please, Sign In to add comment