Skip to content

Commit 0e6ca10

Browse files
committed
feat: add solutions to lc problem: No.0668
No.0668.Kth Smallest Number in Multiplication Table
1 parent 11be672 commit 0e6ca10

File tree

6 files changed

+233
-2
lines changed

6 files changed

+233
-2
lines changed

solution/0600-0699/0668.Kth Smallest Number in Multiplication Table/README.md

+84-1
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,105 @@
4848

4949
<!-- 这里可写通用的实现逻辑 -->
5050

51+
**方法一:二分查找**
52+
53+
题目可以转换为,求有多少个数不超过 x。对于每一行 i,所有数都是 i 的倍数,不超过 x 的个数有 `x / i` 个。
54+
55+
二分枚举 x,累加每一行不超过 x 的个数,得到 cnt。找到 `cnt >= k` 的最小 x。
56+
5157
<!-- tabs:start -->
5258

5359
### **Python3**
5460

5561
<!-- 这里可写当前语言的特殊实现逻辑 -->
5662

5763
```python
58-
64+
class Solution:
65+
def findKthNumber(self, m: int, n: int, k: int) -> int:
66+
left, right = 1, m * n
67+
while left < right:
68+
mid = (left + right) >> 1
69+
cnt = 0
70+
for i in range(1, m + 1):
71+
cnt += min(mid // i, n)
72+
if cnt >= k:
73+
right = mid
74+
else:
75+
left = mid + 1
76+
return left
5977
```
6078

6179
### **Java**
6280

6381
<!-- 这里可写当前语言的特殊实现逻辑 -->
6482

6583
```java
84+
class Solution {
85+
public int findKthNumber(int m, int n, int k) {
86+
int left = 1, right = m * n;
87+
while (left < right) {
88+
int mid = (left + right) >>> 1;
89+
int cnt = 0;
90+
for (int i = 1; i <= m; ++i) {
91+
cnt += Math.min(mid / i, n);
92+
}
93+
if (cnt >= k) {
94+
right = mid;
95+
} else {
96+
left = mid + 1;
97+
}
98+
}
99+
return left;
100+
}
101+
}
102+
```
103+
104+
### **C++**
105+
106+
```cpp
107+
class Solution {
108+
public:
109+
int findKthNumber(int m, int n, int k) {
110+
int left = 1, right = m * n;
111+
while (left < right)
112+
{
113+
int mid = (left + right) >> 1;
114+
int cnt = 0;
115+
for (int i = 1; i <= m; ++i) cnt += min(mid / i, n);
116+
if (cnt >= k) right = mid;
117+
else left = mid + 1;
118+
}
119+
return left;
120+
}
121+
};
122+
```
66123
124+
### **Go**
125+
126+
```go
127+
func findKthNumber(m int, n int, k int) int {
128+
left, right := 1, m*n
129+
for left < right {
130+
mid := (left + right) >> 1
131+
cnt := 0
132+
for i := 1; i <= m; i++ {
133+
cnt += min(mid/i, n)
134+
}
135+
if cnt >= k {
136+
right = mid
137+
} else {
138+
left = mid + 1
139+
}
140+
}
141+
return left
142+
}
143+
144+
func min(a, b int) int {
145+
if a < b {
146+
return a
147+
}
148+
return b
149+
}
67150
```
68151

69152
### **...**

solution/0600-0699/0668.Kth Smallest Number in Multiplication Table/README_EN.md

+80-1
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,97 @@
3535

3636
## Solutions
3737

38+
Binary search.
39+
3840
<!-- tabs:start -->
3941

4042
### **Python3**
4143

4244
```python
43-
45+
class Solution:
46+
def findKthNumber(self, m: int, n: int, k: int) -> int:
47+
left, right = 1, m * n
48+
while left < right:
49+
mid = (left + right) >> 1
50+
cnt = 0
51+
for i in range(1, m + 1):
52+
cnt += min(mid // i, n)
53+
if cnt >= k:
54+
right = mid
55+
else:
56+
left = mid + 1
57+
return left
4458
```
4559

4660
### **Java**
4761

4862
```java
63+
class Solution {
64+
public int findKthNumber(int m, int n, int k) {
65+
int left = 1, right = m * n;
66+
while (left < right) {
67+
int mid = (left + right) >>> 1;
68+
int cnt = 0;
69+
for (int i = 1; i <= m; ++i) {
70+
cnt += Math.min(mid / i, n);
71+
}
72+
if (cnt >= k) {
73+
right = mid;
74+
} else {
75+
left = mid + 1;
76+
}
77+
}
78+
return left;
79+
}
80+
}
81+
```
82+
83+
### **C++**
84+
85+
```cpp
86+
class Solution {
87+
public:
88+
int findKthNumber(int m, int n, int k) {
89+
int left = 1, right = m * n;
90+
while (left < right)
91+
{
92+
int mid = (left + right) >> 1;
93+
int cnt = 0;
94+
for (int i = 1; i <= m; ++i) cnt += min(mid / i, n);
95+
if (cnt >= k) right = mid;
96+
else left = mid + 1;
97+
}
98+
return left;
99+
}
100+
};
101+
```
49102
103+
### **Go**
104+
105+
```go
106+
func findKthNumber(m int, n int, k int) int {
107+
left, right := 1, m*n
108+
for left < right {
109+
mid := (left + right) >> 1
110+
cnt := 0
111+
for i := 1; i <= m; i++ {
112+
cnt += min(mid/i, n)
113+
}
114+
if cnt >= k {
115+
right = mid
116+
} else {
117+
left = mid + 1
118+
}
119+
}
120+
return left
121+
}
122+
123+
func min(a, b int) int {
124+
if a < b {
125+
return a
126+
}
127+
return b
128+
}
50129
```
51130

52131
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int findKthNumber(int m, int n, int k) {
4+
int left = 1, right = m * n;
5+
while (left < right)
6+
{
7+
int mid = (left + right) >> 1;
8+
int cnt = 0;
9+
for (int i = 1; i <= m; ++i) cnt += min(mid / i, n);
10+
if (cnt >= k) right = mid;
11+
else left = mid + 1;
12+
}
13+
return left;
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func findKthNumber(m int, n int, k int) int {
2+
left, right := 1, m*n
3+
for left < right {
4+
mid := (left + right) >> 1
5+
cnt := 0
6+
for i := 1; i <= m; i++ {
7+
cnt += min(mid/i, n)
8+
}
9+
if cnt >= k {
10+
right = mid
11+
} else {
12+
left = mid + 1
13+
}
14+
}
15+
return left
16+
}
17+
18+
func min(a, b int) int {
19+
if a < b {
20+
return a
21+
}
22+
return b
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int findKthNumber(int m, int n, int k) {
3+
int left = 1, right = m * n;
4+
while (left < right) {
5+
int mid = (left + right) >>> 1;
6+
int cnt = 0;
7+
for (int i = 1; i <= m; ++i) {
8+
cnt += Math.min(mid / i, n);
9+
}
10+
if (cnt >= k) {
11+
right = mid;
12+
} else {
13+
left = mid + 1;
14+
}
15+
}
16+
return left;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def findKthNumber(self, m: int, n: int, k: int) -> int:
3+
left, right = 1, m * n
4+
while left < right:
5+
mid = (left + right) >> 1
6+
cnt = 0
7+
for i in range(1, m + 1):
8+
cnt += min(mid // i, n)
9+
if cnt >= k:
10+
right = mid
11+
else:
12+
left = mid + 1
13+
return left

0 commit comments

Comments
 (0)