LeetCode hard 668. Kth Smallest Number in Multiplication Table(二分答案,一次过了,好开心,哈哈哈哈)
题目:https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/description/
668. Kth Smallest Number in Multiplication Table
Nearly every one have used the Multiplication Table. But could you find out the k-th
smallest number quickly from the multiplication table?
Given the height m
and the length n
of a m * n
Multiplication Table, and a positive integer k
, you need to return the k-th
smallest number in this table.
Example 1:
Input: m = 3, n = 3, k = 5
Output:
Explanation:
The Multiplication Table:
1 2 3
2 4 6
3 6 9 The 5-th smallest number is 3 (1, 2, 2, 3, 3).
Example 2:
Input: m = 2, n = 3, k = 6
Output:
Explanation:
The Multiplication Table:
1 2 3
2 4 6 The 6-th smallest number is 6 (1, 2, 2, 3, 4, 6).
Note:
- The
m
andn
will be in the range [1, 30000]. - The
k
will be in the range [1, m * n]
二分答案,用lower_bound和upper_bound的思想。
这居然是一道hard题,难以置信,就这个难度啊。
我被自己蠢哭了,把它当成ACM题,以为时间只有一秒,非要优化到O(n*log(n))。
没想到时间和空间复杂度O(n*m)就可以过了。
不说了,第一次过hard题,20分钟,直接上代码把:
class Solution {
public:
int findKthNumber(int m, int n, int k) {
if(m > n) swap(m, n);
int l = ; int r = m*n;
while(l < r){
int mid = (l+r)/;
int p = judge(m, n, k, mid);
// cout << p << " ";
if(p == ){
return mid;
}else if(p == ){
r = mid-;
}else if(p == ){
l = mid+;
}
}
return r;
}
int judge(int m, int n, int k, int mid){
int sum1 = , sum2 = ;
for(int i = ;i <= m; i++){
if(i * n <= mid){
sum2 += n;
}else{
sum2 += mid/i;
} if(i * n < mid){
sum1 += n;
}else{
sum1 += (mid-)/i;
}
// cout << sum1 << endl;
}
cout << mid << " " << sum1 << " " << sum2 << endl;
if(sum1 < k && sum2 >= k){
return ;
}else if(sum1 >= k){
return ;
}else if(sum2 < k){
return ;
}
}
};
LeetCode hard 668. Kth Smallest Number in Multiplication Table(二分答案,一次过了,好开心,哈哈哈哈)的更多相关文章
- 【leetcode】668. Kth Smallest Number in Multiplication Table
题目如下: 解题思路:几乎和[leetcode]719. Find K-th Smallest Pair Distance 的方法一样.只不过一个是减法一个是乘法,还有一点区别是[leetcode]7 ...
- 668. Kth Smallest Number in Multiplication Table
Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number ...
- [LeetCode] Kth Smallest Number in Multiplication Table 乘法表中的第K小的数字
Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number ...
- [Swift]LeetCode668. 乘法表中第k小的数 | Kth Smallest Number in Multiplication Table
Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number ...
- 排序矩阵中的从小到大第k个数 · Kth Smallest Number In Sorted Matrix
[抄题]: 在一个排序矩阵中找从小到大的第 k 个整数. 排序矩阵的定义为:每一行递增,每一列也递增. [思维问题]: 不知道应该怎么加,因为不是一维单调的. [一句话思路]: 周围两个数给x或y挪一 ...
- [LeetCode] 719. Find K-th Smallest Pair Distance 找第K小的数对儿距离
Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pai ...
- [LintCode] Kth Smallest Number in Sorted Matrix 有序矩阵中第K小的数字
Find the kth smallest number in at row and column sorted matrix. Have you met this question in a rea ...
- Lintcode: Kth Smallest Number in Sorted Matrix
Find the kth smallest number in at row and column sorted matrix. Example Given k = 4 and a matrix: [ ...
- Lintcode401 Kth Smallest Number in Sorted Matrix solution 题解
[题目描述] Find the kth smallest number in at row and column sorted matrix. 在一个排序矩阵中找从小到大的第 k 个整数. 排序矩阵的 ...
随机推荐
- Load和CPU利用率是如何算出来的
相信很多人都对Linux中top命令里“load average”这一栏困惑过,到底什么是Load,Load代表了什么含义,Load高会有什么后果?“%CPU”这一栏为什么会超过100%,它是如何计算 ...
- MVC中添加模块区域,并设置RedirectToAction跳转
废话少说,直接上图:
- js闭包概念
含义:闭包是一个概念,它描述了函数执行完毕内存释放后,依然内存驻留的一个现象,只要把握这个核心概念,闭包就不难理解了 function a(){ var i=0; function ...
- PHP 导出excel 数据量大时
public function ceshiexcel1(){ set_time_limit(0); $filename = '病毒日志'; header('Content-Type: applicat ...
- ZBrush中Mrgb、Rgb和M的使用
ZBrush®软件工具架中所有的命令选项都是我们平时较为常用的,位于工具架的中间位置,有一个Mrgb.Rgb和M选项它们所表示的是材质及颜色,那么,在Zbrush中只要选择相应的选项,就可以同时绘制材 ...
- 2017CCPC秦皇岛
热身赛 B题 Smartphone: 大整数相乘 Time Limit: 1 Second Memory Limit: 65536 KBHelianthuswolf Co. Ltd. is a mul ...
- 关于Number的属性和方法你知晓几分?速来围观!
1.Number.isFinite() 方法用来检测传入的参数是否是一个有穷数(finite number)返回值为布尔值. 和全局的 isFinite() 函数相比,这个方法不会强制将一个非数值的参 ...
- cmake模板
1.主要命令 project (TEST):指定项目名称为TEST aux_source_directory(<dir> <variable>):将当前目录中的源文件名称赋值给 ...
- Shiro:初识Shiro及简单尝试
Shiro 一.什么是Shiro Apache Shiro是Java的一个安全(权限)框架 作用:认证.授权.加密.会话管理.与web集成.缓存等 下载地址:http://shiro.apache.o ...
- 大道至简第一章读后感 Java伪代码形式
观看了大道至简的第一章之后,从愚公移山的故事中我们可以抽象出一个项目, 下面用Java 伪代码的形式来进行编写: import java(愚公移山的故事) //愚公移山 public class yu ...