Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int findKthNumber(int m, int n, int k) {
- int rows = m, cols = n, l=1, r = m*n;
- // Binary search on answer space.
- int ans = 0;
- while(l<r){
- int mid = (l+r)/2;
- int count = getCount(mid, rows, cols);
- if(count < k)
- l = mid+1;
- else
- r = mid;
- }
- return l;
- }
- int getCount(int k, int rows, int cols){
- int count = 0, curX = rows-1, curY = 0;
- // Count numbers less-equal k.
- for(int i=1; i<=rows; i++)
- count += min(k/i, cols);
- return count;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment