c++ solution beats 100%


  • 0
    H

    binary search

    class Solution {
    public:
        int findKthNumber(int m, int n, int k) {
            int l = 1, r = m*n, mid, res;
            while(l<=r)
            {
                mid = (l+r) >> 1;
                int cnt = 0;
                for(int i = 1; i <= m; i++) cnt += min(mid / i, n);
                if(cnt >= k)
                {
                    res = mid;
                    r = mid - 1;
                }
                else if(cnt < k)
                    l = mid + 1;
            }      
            return res;
        }
    };
    

Log in to reply
 

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