LeetCode hard 668. Kth Smallest Number in Multiplication Table(二分答案)
题目:https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/description/
- 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 and n 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 = 1; int r = m*n;while(l < r){int mid = (l+r)/2;int p = judge(m, n, k, mid);// cout << p << " ";if(p == 1){return mid;}else if(p == 2){r = mid-1;}else if(p == 3){l = mid+1;}}return r;}int judge(int m, int n, int k, int mid){int sum1 = 0, sum2 = 0;for(int i = 1;i <= m; i++){if(i * n <= mid){sum2 += n;}else{sum2 += mid/i;}if(i * n < mid){sum1 += n;}else{sum1 += (mid-1)/i;}// cout << sum1 << endl;}cout << mid << " " << sum1 << " " << sum2 << endl;if(sum1 < k && sum2 >= k){return 1;}else if(sum1 >= k){return 2;}else if(sum2 < k){return 3;}}
};
LeetCode hard 668. Kth Smallest Number in Multiplication Table(二分答案)相关推荐
- leetcode 668. Kth Smallest Number in Multiplication Table | 668. 乘法表中第k小的数(二分查找)
题目 https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/ 题解 看了答案 Approach #3: B ...
- 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: [ ...
- 算法设计与分析第一次作业 1300. k-th Smallest Number
题目描述 给定 nnn 个正整数,请找出其中的第 kkk 小的数. 输入可能有重复数字,此时第kkk 小的值定义为唯一的 xxx,满足 公式 (∣{y∣y<x}∣<k)∧(∣{y∣y≥x} ...
- 378. Kth Smallest Element in a Sorted Matrix
文章目录 1题目理解 2 思路分析 2.1二分思路 2.2计算小于等于middle值的个数 3 拓展解决leetcode 668 1题目理解 输入:一个nxn的矩阵,每一行从左到右按照升序排列,每一列 ...
- Kth Smallest Element in a Sorted Matrix
类似的题目有: 373. Find K Pairs with Smallest Sums 378. Kth Smallest Element in a Sorted Matrix 668. Kth S ...
- LeetCode 230. Kth Smallest Element in a BST--C++,Python解法--面试真题--找二叉树中第K小的元素
题目地址:Kth Smallest Element in a BST - LeetCode Given a binary search tree, write a function kthSmalle ...
- [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素
题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...
- 【LeetCode 剑指offer刷题】树题16:Kth Smallest Element in a BST
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Kth Smallest Element in a BST Given a binary search tree, ...
- leetcode【537】Complex Number Multiplication(复数相乘)
写在最前面:一道很常规的字符串分割的题 leetcode[537]Complex Number Multiplication Given two strings representing two co ...
最新文章
- Duplicate entry '0' for key 'PRIMARY'_Spring Batch
- IPv6中的特殊地址
- svn 迁移到 git 仓库并保留 commit 历史记录
- 关于Yii2中CSS,JS文件的引入心得
- waitpid()函数
- 解决导出word迅雷读取地址和下载出错
- eclipse导入myeclipse项目
- android系统(107)---Android路由表设置(route amp; DNS)
- 又一次摔MFC坑里了
- python_四元数q转旋转矩阵R(已验证)
- 8月18日梦记录及分析
- 各种格式的地图瓦片下载
- 主键中的mappdeBy的使用方法
- VHDL-任意分频器(50%占空比)
- 算法学习:插值型求积公式
- Js打开QQ聊天对话窗口
- win10系统 没有wifi图标 WiFi列表没有显示
- Error,java对常量池来说字符串xxx的UTF8表示过长
- Redis 数据操作
- NLP(自然语言处理) - Tricks Dataset 集合
热门文章
- Java黑皮书课后题第6章:**6.3(回文整数)使用下面的方法头编写两个方法:……使用reverse方法实现isPalindrome。如果一个数字的逆序数和它自身相等,这个数就称为回文数。
- mysql游标遍历修改_mysql使用游标遍历数据进行批量针对性更新数据,急求mysql大神解答...
- 迅雷2012校园招聘笔试题
- BZOJ 3991: [SDOI2015]寻宝游戏
- 设计模式系列6:适配器模式(Adapter Pattern)
- php模拟post提交请求与调用接口
- spring注解方式 idea报could not autowire
- const 内联 枚举 宏
- sql 合并相同条件的字段
- 类实现Java模板方法模式中的HookMethod实现