博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 668: Kth Smallest Number in Multiplication Table
阅读量:7047 次
发布时间:2019-06-28

本文共 1010 字,大约阅读时间需要 3 分钟。

Note:

Basic idea could be same as Kth smallest number in sorted matrix.

But this could be different since all the numbers fall in [1, m*n + 1] range. But not all the numbers in the range belongs to the multiplication table.

So we need a helper function to calculate how many numbers from [1, mid]. It is counts += Math.min(value / i, n), since max value no larger than m.

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

 

转载于:https://www.cnblogs.com/shuashuashua/p/7500858.html

你可能感兴趣的文章
010——VUE中使用lodash库减少watch对后台请求的压力
查看>>
Yii框架上传后展示图片
查看>>
EXCEL教程,包你一学就会
查看>>
二叉堆 - 最小堆
查看>>
Linux 定时任务
查看>>
appium脚本报错selenium.common.exceptions.WebDriverException
查看>>
java udp与tcp
查看>>
fiddler
查看>>
非常值得学习的java 绘图板源代码
查看>>
Sql Server 语句
查看>>
SharePoint 2013下,使用ajax调用ashx报Http 302错误
查看>>
proxool数据连接池
查看>>
ASP.NET 访问 MySql
查看>>
NS 2.35 柯志亨书-实验4笔记-随机数产生-参数化批处理
查看>>
ios 导航问题
查看>>
[Android学习笔记]使用getIdentifier()获取资源Id
查看>>
Vim与Python真乃天作之合
查看>>
阅读《移山之道》及讲义感想
查看>>
css3实现好看的边框效果
查看>>
Difference Between 2 Dates or 2 Times
查看>>