668. Kth Smallest Number in Multiplication Table
阿新 • • 發佈:2020-08-16
問題:
在以下構成的Multiplication Table中,找到第K個小的數字。
The Multiplication Table: m=3, n=3 1 2 3 2 4 6 3 6 9
The Multiplication Table: m=4,n=5
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
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]
解法:二分查詢(Binary Search)
找到最小的mid,使得mid以前的數字個數>=k。
- 最小值:l:1
- 最大值:r:n*m
我們需要首先實現函式,取得mid以前的數字個數。
Multiplication Table的特點是:
第 i 行,是以 i 為公差的等差數列。
那麼,對於每行,若mid/i(行號)=該行有多少個小於mid的數。
但同時,若列數n<上面的個數,那麼最大取列數n
則有,每行<mid的個數 count+=min(n, mid/i)
逐行累加。
int getCountSmallerMid(int m, int n, int mid)
程式碼參考:
class Solution { public: int getCountSmallerMid(int m, int n, int mid) { int count = 0; for(int i=1 ;i<=m; i++) { count+=min(n,mid/i); } return count; } int findKthNumber(int m, int n, int k) { int l = 1, r = n*m+1; while(l<r) {int mid = l+(r-l)/2; if(getCountSmallerMid(m, n, mid)>=k) { r = mid; } else { l = mid+1; } } return l; } };