Skip to content

Commit 21969f1

Browse files
add 668
1 parent 83c41df commit 21969f1

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
25+
|668|[Kth Smallest Number in Multiplication Table](https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_668.java) | O(logm*n) | O(1) | Hard | Binary Search
2526
|666|[Path Sum IV](https://leetcode.com/problems/path-sum-iv/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_666.java) | O(1) | O(1) | Medium | Tree, DFS
2627
|665|[Non-decreasing Array](https://leetcode.com/problems/non-decreasing-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_665.java) | O(n) | O(n) | Easy |
2728
|664|[Strange Printer](https://leetcode.com/problems/strange-printer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_664.java) | O(n^3) | O(n^2) | Hard | DP
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.PriorityQueue;
4+
5+
/**
6+
* 668. Kth Smallest Number in Multiplication Table
7+
*
8+
* Nearly every one have used the Multiplication Table.
9+
* But could you find out the k-th smallest number quickly from the multiplication table?
10+
* Given the height m and the length n of a m * n Multiplication Table,
11+
* and a positive integer k, you need to return the k-th smallest number in this table.
12+
13+
Example 1:
14+
Input: m = 3, n = 3, k = 5
15+
Output:
16+
Explanation:
17+
The Multiplication Table:
18+
1 2 3
19+
2 4 6
20+
3 6 9
21+
22+
The 5-th smallest number is 3 (1, 2, 2, 3, 3).
23+
24+
25+
Example 2:
26+
Input: m = 2, n = 3, k = 6
27+
Output:
28+
Explanation:
29+
The Multiplication Table:
30+
1 2 3
31+
2 4 6
32+
33+
The 6-th smallest number is 6 (1, 2, 2, 3, 4, 6).
34+
35+
Note:
36+
37+
The m and n will be in the range [1, 30000].
38+
The k will be in the range [1, m * n]
39+
*/
40+
41+
public class _668 {
42+
public static class Solution1 {
43+
/**
44+
* This brute force approach resulted in
45+
* TLE on Leetcode and
46+
* OOM error by _668test.test3() when running in my localhost:
47+
* java.lang.OutOfMemoryError: Java heap space
48+
* at java.util.Arrays.copyOf(Arrays.java:3210)
49+
* at java.util.Arrays.copyOf(Arrays.java:3181)
50+
* at java.util.PriorityQueue.grow(PriorityQueue.java:300)
51+
* at java.util.PriorityQueue.offer(PriorityQueue.java:339)
52+
*/
53+
public int findKthNumber(int m, int n, int k) {
54+
PriorityQueue<Integer> minHeap = new PriorityQueue<>((a, b) -> a - b);
55+
for (int i = 1; i <= m; i++) {
56+
for (int j = 1; j <= n; j++) {
57+
minHeap.offer(i * j);
58+
}
59+
}
60+
while (k-- > 1) {
61+
minHeap.poll();
62+
}
63+
return minHeap.peek();
64+
}
65+
}
66+
67+
public static class Solution2 {
68+
/**reference: https://discuss.leetcode.com/topic/101132/java-solution-binary-search*/
69+
public int findKthNumber(int m, int n, int k) {
70+
int low = 1;
71+
int high = m * n + 1;
72+
while (low < high) {
73+
int mid = low + (high - low) / 2;
74+
int c = count(mid, m, n);
75+
if (c >= k) {
76+
high = mid;
77+
} else {
78+
low = mid + 1;
79+
}
80+
}
81+
return high;
82+
}
83+
84+
int count(int v, int m, int n) {
85+
int count = 0;
86+
for (int i = 1; i <= m; i++) {
87+
int temp = Math.min(v / i, n);
88+
count += temp;
89+
}
90+
return count;
91+
}
92+
}
93+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._668;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _668Test {
10+
private static _668.Solution1 solution1;
11+
private static _668.Solution2 solution2;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _668.Solution1();
16+
solution2 = new _668.Solution2();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
assertEquals(3, solution1.findKthNumber(3, 3, 5));
22+
assertEquals(3, solution2.findKthNumber(3, 3, 5));
23+
}
24+
25+
@Test
26+
public void test2() {
27+
assertEquals(6, solution1.findKthNumber(2, 3, 6));
28+
assertEquals(6, solution2.findKthNumber(2, 3, 6));
29+
}
30+
31+
@Test
32+
public void test3() {
33+
// assertEquals(31666344, solution1.findKthNumber(9895, 28405, 100787757));//this will run into OOM error, so comment out
34+
assertEquals(31666344, solution2.findKthNumber(9895, 28405, 100787757));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)