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
mandnwill be in the range [1, 30000]. - The
kwill 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 个整数. 排序矩阵的 ...
随机推荐
- Redis学习笔记(七) 基本命令:Set操作
原文链接:http://doc.redisfans.com/set/index.html 虽然set和list很相似但还是有一些差别的,如set中的顺序没有先后之分,所以不像list一样可以在首尾增删 ...
- Codeforces Round #493 (Div. 2) A. Balloons 贪心水题
由于是输出任意一组解,可以将价值从小到大进行排序,第一个人只选第一个,第二个人选其余的.再比较一下第一个人选的元素和第二个人所选元素和是否相等即可.由于已将所有元素价值从小到大排过序,这样可以保证在有 ...
- JS 日历
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...
- IOS - JSON数据解析 小3种方法
[manager GET:serverURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject ...
- 利用Arcade表达式显示多行标签
要素图层依然是全球气象站点分布数据,属性表如下: 展示效果如下,显示的四行数据分别是属性表中的WIND_NAME,TEMP,WIND(运算之后的),R_HUMIDITY 本次尝试一次性写全所有的信息: ...
- ansible 定义主机用户和密码
定义主机组用户和密码 [webservers] ansible[01:04] ansible_ssh_user='root' ansible_ssh_pass='AAbb0101' [root@ftp ...
- vue无缝滚动的插件开发填坑分享
写插件的初衷 1.项目经常需要无缝滚动效果,当时写jq的时候用用msClass这个老插件,相对不上很好用. 2.后来转向vue在vue-awesome没有找到好的无缝滚动插件,除了配置swiper可以 ...
- vue-router的创建(1)
vue-router的创建 <!doctype html> <html lang="en"> <head> <meta charset=& ...
- Spring IoC简介及使用
Spring根本任务 Spring的根本任务就是简化Java开发. 目前许多框架如果要使用他们,就必须要继承或实现这些框架的各种类.这使得框架与我们的程序耦合度过高.由于在我们的程序中加入了过多的框架 ...
- echarts 柱状图和饼状图动态获取后台数据
运用echarts来实现图表 1.首先下载echarts包 http://echarts.baidu.com/echarts2/doc/example.html,在这里我下载的是 2.将echart ...