当前位置:网站首页>力扣解法汇总668-乘法表中第k小的数
力扣解法汇总668-乘法表中第k小的数
2022-06-12 02:03:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
几乎每一个人都用 乘法表。但是你能在乘法表中快速找到第k小的数字吗?
给定高度m 、宽度n 的一张 m * n的乘法表,以及正整数k,你需要返回表中第k 小的数字。
例 1:
输入: m = 3, n = 3, k = 5
输出: 3
解释:
乘法表:
1 2 3
2 4 6
3 6 9
第5小的数字是 3 (1, 2, 2, 3, 3).
例 2:
输入: m = 2, n = 3, k = 6
输出: 6
解释:
乘法表:
1 2 3
2 4 6
第6小的数字是 6 (1, 2, 2, 3, 4, 6).
注意:
m 和 n 的范围在 [1, 30000] 之间。
k 的范围在 [1, m * n] 之间。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/kth-smallest-number-in-multiplication-table
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 这题我没想出答案,最终看题解才想通的。 * 我还是有一个疑惑,每次二分法查找比middle小的数有几个的时候,怎么保证最后的那个middle+1或者right一定是乘法表中的数的?
代码:
public class Solution668 {
public int findKthNumber(int m, int n, int k) {
int left = 1;
int right = m * n;
int middle = 0;
while (left < right) {
middle = left + (right - left) / 2;
//求小于middle的数量
int count = 0;
for (int i = 1; i <= m; i++) {
int min = Math.min(m, middle / i);
count += min;
}
if (count >= k) {
right = middle;
continue;
}
left = middle + 1;
}
return left;
}
}边栏推荐
- How WPS inserts a directory and the operating steps for quickly inserting a directory
- Ozzanation - système d'action basé sur sse
- How to automatically color cells in Excel
- Basic use of MATLAB
- “中国东信杯”广西大学第四届程序设计竞赛(同步赛)
- 力扣解法汇总473-火柴拼正方形
- 关于vagrant up的一个终结之谜
- [learn FPGA programming from scratch -19]: quick start chapter - operation steps 4-1- Verilog software download and construction of development environment - Altera quartz II version
- 如何定位关键词使得广告精准投放。
- Google Ads 竞价的运作机制
猜你喜欢

In 2022, the internal promotion of the "MIHA Tour" golden, silver and silver social recruitment started in April and march! Less overtime, good welfare, 200+ posts for you to choose, come and see!

如何最大化的利用各种匹配方式? ——Google SEM

Basic use of MATLAB

MySQL表常用操作思维导图

The release of star ring kundb 2.2 provides a new choice for business systems with high concurrent transactions and queries

Graphical data analysis | business analysis and data mining

Tiobe - programming language ranking in June 2022

matplotlib. pyplot. Bar chart (II)

php开发 博客系统的公告模块的建立和引入

SQL calculates KS, AUC, IV, psi and other risk control model indicators
随机推荐
小程序111111
Wechat applet - a case of comparing the size of numbers
2022最全面的Redis事务控制(带图讲解)
C asynchronous programming from simple to deep (III) details awaiter
How to improve the advertising rating of advertising, that is, the quality score?
Implementation scheme of iteration and combination pattern for general tree structure
Explore performance optimization! Performance improvement from 2 months to 4 hours!
Smartbi helps you solve the problem of losing high-value customers
自适应搜索广告有哪些优势?
The establishment and introduction of the announcement module of PHP development blog system
[learn FPGA programming from scratch -20]: quick start chapter - operation steps 4-2-quick use of Altera quartz II tool (Modelsim co simulation, program download to altera development board)
MySQL表常用操作思维导图
Four schemes for redis to implement message queue
国资入股,建业地产这回稳了吗?
Redis cluster + sentinel mode + replicas
MySQL table common operation mind map
matplotlib. pyplot. Bar chart (II)
力扣解法汇总-剑指 Offer II 114. 外星文字典
lua 函数
Google Ads 竞价的运作机制