solution like Kth Smallest Number in Sorted Matrix


  • 2
    S

    class Solution {
    public int findKthNumber(int m, int n, int k) {
    int left = 1 * 1;
    int right = m * n;
    while (left < right) {
    int mid = left + (right - left) / 2;
    int count = count(mid, m, n);
    if (count < k) {
    left = mid + 1;
    } else {
    right = mid;
    }
    }
    return right;
    }

    private int count(int value, int m, int n) {
        int i = m, j = 1;
        int count = 0;
        while (i >= 1 && j <= n) {
            if (i * j <= value) {
                count += i;
                j++;
            } else {
                i--;
            }
        }
        return count;
    }
    

    }


Log in to reply
 

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