Easy to Understand Java Solution


  • 1

    The helper function is learned from @shawngao solution(https://discuss.leetcode.com/topic/101132/java-solution-binary-search/6), much efficient than what I initially thought...

    class Solution {
        public int findKthNumber(int m, int n, int k) {
            int low = 1, high = m * n;
            while (low <= high) {
                int mid = low + (high - low) / 2;
                int count = helper(m, n, mid);
                if (count >= k) high = mid - 1;
                else low = mid + 1;
            }
            return low;
        }
        private int helper(int m, int n, int num) {
            int count = 0;
            for (int i = 1; i <= m; i++) {
                count += Math.min(num / i, n);
            }
            return count;
        }
    }
    

Log in to reply
 

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