Skip to content

Commit 365dd79

Browse files
authored
Update 668-kth-smallest-number-in-multiplication-table.js
1 parent 29724db commit 365dd79

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

668-kth-smallest-number-in-multiplication-table.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,33 @@ function count(m, n, target) {
3030
}
3131
return nSmaller;
3232
}
33+
34+
// another
35+
36+
/**
37+
* @param {number} m
38+
* @param {number} n
39+
* @param {number} k
40+
* @return {number}
41+
*/
42+
const findKthNumber = function(m, n, k) {
43+
let left = 1;
44+
let right = m * n;
45+
while (left < right) {
46+
const mid = Math.floor((left + right) / 2);
47+
const num = count(m, n, mid);
48+
if (num < k) left = mid + 1;
49+
else right = mid;
50+
}
51+
return left;
52+
};
53+
54+
function count(m, n, target) {
55+
let res = 0;
56+
let j = n;
57+
for (let i = 1; i <= m; i++) {
58+
while (i * j > target) j--
59+
res += j;
60+
}
61+
return res;
62+
}

0 commit comments

Comments
 (0)