[Leetcode]668.Kth Smallest Number in Multiplication Table

链接:LeetCode668

给定高度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).

相关标签:二分查找

乘法表的特点是每行和每列元素均按升序排序,这题就可以转换为[LeetCode]378.Kth Smallest Element in a Sorted Matrix。我们通过二分查找不断逼近真实值即可。
代码如下:

python:

class Solution:
    def findKthNumber(self, m: int, n: int, k: int) -> int:
        lo, hi = 1, m*n
        while lo <= hi:
            mid = (lo + hi) >> 1
            loc = self.countLower(m,n, mid)
            if loc < k:
                lo = mid + 1
            else:
                hi = mid - 1
        return lo

    def countLower(self, m,n, num):
        i, j = 1,n
        cnt = 0
        while i <= m and j >= 1:
            if i*j <= num:
                cnt += j
                i += 1
            else:
                j -= 1
        return cnt

C++:

class Solution {
public:
    int findKthNumber(int m, int n, int k) {
        int lo=1,hi=m*n;
        while(lo<=hi){
            mid = lo+((hi-lo)>>1);
            cnt = countSamller(m,n,mid);
            if(k <= cnt){
                hi = mid-1;
            } else {
                lo = mid+1;
            }
        }
        return lo;
    }

    int countSamller(int m,int n,int target){
        int i=1,j=n;
        int cnt = 0;
        while(i<=m && j>=1){
            if(target<(i*j)){
                j--;
            }else{
                cnt += j;
                i++;
            }
        }
        return cnt;
    }
};

原文地址:https://www.cnblogs.com/hellojamest/p/12254695.html

时间: 2024-07-31 18:46:56

[Leetcode]668.Kth Smallest Number in Multiplication Table的相关文章

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 ret

[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 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 ret

[Lintcode] Kth Smallest Number in Sorted Matrix

Kth Smallest Number in Sorted Matrix Find the kth smallest number in at row and column sorted matrix. Have you met this question in a real interview? Yes Example Given k = 4 and a matrix: [ [1 ,5 ,7], [3 ,7 ,8], [4 ,8 ,9], ] return 5 Challenge O(k lo

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: [ [1 ,5 ,7], [3 ,7 ,8], [4 ,8 ,9], ] return 5. 分析: Although the matrix is horizontally and vertically sorted, there is no rule to find the next smalles

[LeetCode][JavaScript]Kth Smallest Element in a BST

Kth Smallest Element in a BST Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. Follow up: What if the BST is modified (insert/delete

【Leetcode】Kth Smallest Element in a Sorted Matrix

题目链接:https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/ 题目: Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix. Note that it is the kth smallest eleme

(medium)LeetCode 230.Kth Smallest Element in a BST

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. Follow up:What if the BST is modified (insert/delete operations) often and you need

leetcode 378. Kth Smallest Element in a Sorted Matrix

Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix. Note that it is the kth smallest element in the sorted order, not the kth distinct element. Example: matrix = [ [ 1, 5

[LeetCode] 230. Kth Smallest Element in a BST 解题思路

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. 问题:找出二叉搜索树种第 k 小的元素. 一个深度遍历的应用.使用递归.或者借助栈都可以实现深度遍历.本文代码使用递归实现. 1 void visit(TreeNod