|
| 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 | +} |
0 commit comments