0
点赞
收藏
分享

微信扫一扫

每日一题 leetcode 668. 乘法表中第k小的数 java

钵仔糕的波波仔 2022-05-18 阅读 67

卢梭曾经说过:“人生而自由,又不往在枷锁中。” 但我仍要说:不自由,毋宁死。

二分查找
https://leetcode.cn/problems/kth-smallest-number-in-multiplication-table/

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

相关推荐

0 条评论