LeetCode 668. Kth Smallest Number in Multiplication Table
来源:互联网 发布:英雄联盟网络连接断开 编辑:程序博客网 时间:2024/05/22 05:59
https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/description/
一、题目:
给出一个 m*n 的乘法表,然后求出第k大的数字。
二、分析:
1、最开始的思路,是便利一下 m*n, 然后用 map 容器把每个节点出现的个数保存起来, map[node]++; 结果超时。
2、用Bs,进行查找,在 1~m*n 之间进行二分查找,如果 1~mid 之间不大于 k 的节点的个数大于 k, 则继续二分左半部分;反之,二分右半部分。
其中最为纠结的就是在二分的时候,如何判断一个居间内 不大于 k 的节点个数。
三、map 容器的超时代码:
#include<iostream>#include <map>using namespace std;#define MaxLen 30010class Solution {public: int findKthNumber(int m, int n, int k) { map<int, int>_map; _map.clear(); for(int i=1; i<=m; i++){ for(int j=1; j<=n; j++){ int tmp = 0; if(i==1){ tmp = j; }else if(j==1){ tmp = i; }else{ tmp = i*j; } _map[tmp]++; } } int ans = 0; for(map<int ,int>::iterator iter = _map.begin(); iter!=_map.end(); iter++){ ans += iter->second; if(ans>=k){ ans = iter->first; break; } } cout<<ans<<endl; return ans; }};int main(){ int m, n, k; map<int, int>_m; cin>>m>>n>>k; Solution s; s.findKthNumber(m, n, k); return 0;}
四、Bs 的Ac 代码:
#include<iostream>#include <map>using namespace std;#define MaxLen 30010class Solution {public: //获取不大于k 的节点的个数 int getSmallerNum(int m, int n, int value){ int ans = 0; for(int i=1; i<=m; i++){ ans += min(n, value/i); } return ans; } int findKthNumber(int m, int n, int k) { if(k==m*n) return k; int left = 1, right = m*n; int ans = 0; while(left<=right){ int mid = (right-left)/2 + left; int num = getSmallerNum(m, n, mid); if(num>=k){ ans = mid; right = right-1; }else{ left = left+1; } } cout<<ans<<endl; return ans; }};int main(){ int m, n, k; cin>>m>>n>>k; Solution s; s.findKthNumber(m, n, k); return 0;}
阅读全文
0 0
- [leetcode]668. Kth Smallest Number in Multiplication Table
- LeetCode 668. Kth Smallest Number in Multiplication Table
- leetcode题解-668. Kth Smallest Number in Multiplication Table
- 668. Kth Smallest Number in Multiplication Table
- [leetcode] 668 Kth Smallest Number in Multiplication Table
- Kth Smallest Number in Multiplication Table
- leetcode 668. Kth Smallest Number in Multiplication Table 有序矩阵搜索
- LeetCode hard 668. Kth Smallest Number in Multiplication Table(二分答案,一次过了,好开心,哈哈哈哈)
- 668[Hard]:Kth Smallest Number in Multiplication Table
- leetcode_668. Kth Smallest Number in Multiplication Table ? 待解决
- LintCode_401 Kth Smallest Number in Sorted Matrix
- #leetcode#Kth Smallest Element in a BST
- leetcode--Kth Smallest Element in a BST
- Leetcode|Kth Smallest Element in a BST
- [leetcode] Kth Smallest Element in a BST
- 【LeetCode】Kth Smallest Element in a BST
- LeetCode Kth Smallest Element in a BST
- 【leetcode】Kth Smallest Element in a BST
- PHP常用函数
- Android的消息循环机制 Looper Handler类分析
- RecyclerView使用,item分割线,全选反选按钮实现
- CountDownLatch
- 淘宝新店如何破零
- LeetCode 668. Kth Smallest Number in Multiplication Table
- css---flex布局中,如何响应式 得水平垂直居中?flex子元素左右 上下居中
- 面向对象相关问题
- Duplicate entry '??…???' for key 'cname' Query:
- 关于c++空指针0/NULL/nullptr三者的演变
- 前端面试之---javascript原型
- fatal: the remote end hung up unexpectedly (curl 56 OpenSSL SSL_read:SSL_ERROR_sysCALL)
- 大数据协作框架之Oozie
- python安装与初步使用教程