Skip to content

Commit a96686d

Browse files
committed
Add solution #668
1 parent ada135e commit a96686d

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@
501501
662|[Maximum Width of Binary Tree](./0662-maximum-width-of-binary-tree.js)|Medium|
502502
664|[Strange Printer](./0664-strange-printer.js)|Hard|
503503
667|[Beautiful Arrangement II](./0667-beautiful-arrangement-ii.js)|Medium|
504+
668|[Kth Smallest Number in Multiplication Table](./0668-kth-smallest-number-in-multiplication-table.js)|Hard|
504505
680|[Valid Palindrome II](./0680-valid-palindrome-ii.js)|Easy|
505506
684|[Redundant Connection](./0684-redundant-connection.js)|Medium|
506507
686|[Repeated String Match](./0686-repeated-string-match.js)|Easy|
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* 668. Kth Smallest Number in Multiplication Table
3+
* https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/
4+
* Difficulty: Hard
5+
*
6+
* Nearly everyone has used the Multiplication Table. The multiplication table of size m x n is
7+
* an integer matrix mat where mat[i][j] == i * j (1-indexed).
8+
*
9+
* Given three integers m, n, and k, return the kth smallest element in the m x n multiplication
10+
* table.
11+
*/
12+
13+
/**
14+
* @param {number} m
15+
* @param {number} n
16+
* @param {number} k
17+
* @return {number}
18+
*/
19+
var findKthNumber = function(m, n, k) {
20+
let low = 1;
21+
let high = m * n;
22+
23+
while (low < high) {
24+
const middle = Math.floor((low + high) / 2);
25+
if (helper(middle) < k) {
26+
low = middle + 1;
27+
} else {
28+
high = middle;
29+
}
30+
}
31+
32+
return low;
33+
34+
function helper(x) {
35+
let count = 0;
36+
for (let i = 1; i <= m; i++) {
37+
count += Math.min(Math.floor(x / i), n);
38+
}
39+
return count;
40+
}
41+
};

0 commit comments

Comments
 (0)