My 8 Lines C++ Solution


  • 2
    G
    class Solution {
    public:
        int findKthNumber(int m, int n, int k) {
            int left = 1, right = m * n;
            while (left < right) {
                int mid = left + (right - left) / 2, cnt = 0;
                for (int i = 1; i <= m; ++i) {
                    cnt += (mid / i <= n) ? (mid / i) : n;
                }
                if (cnt < k) left = mid + 1;
                else right = mid;
            }
            return left;
        }
    };

Log in to reply
 

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