Tuesday, August 29, 2017

LeetCode 668 - Kth Smallest Number in Multiplication Table

https://leetcode.com/problems/kth-smallest-number-in-multiplication-table
Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number quickly from the multiplication table?
Given the height m and the length n of a m * n Multiplication Table, and a positive integer k, you need to return the k-th smallest number in this table.
Example 1:
Input: m = 3, n = 3, k = 5
Output:
Explanation:
The Multiplication Table:
1 2 3
2 4 6
3 6 9

The 5-th smallest number is 3 (1, 2, 2, 3, 3).

Example 2:
Input: m = 2, n = 3, k = 6
Output:
Explanation:
The Multiplication Table:
1 2 3
2 4 6

The 6-th smallest number is 6 (1, 2, 2, 3, 4, 6).

Note:
1. The m and n will be in the range [1, 30000].
2. The k will be in the range [1, m * n]
X.
https://discuss.leetcode.com/topic/101132/java-solution-binary-search
It counts all values smaller or equal than v in each column I guess. Using min is to avoid the count is larger than row number. It's a nice solution.
    public int findKthNumber(int m, int n, int k) {
int low = 1 , high = m * n + 1;

while (low < high) {
int mid = low + (high - low) / 2;
int c = count(mid, m, n);
if (c >= k) high = mid;
else low = mid + 1;
}

return high;
}

private int count(int v, int m, int n) {
int count = 0;
for (int i = 1; i <= m; i++) {
int temp = Math.min(v / i , n);
count += temp;
}
return count;
}

https://stackoverflow.com/questions/33464901/using-binary-search-to-find-k-th-largest-number-in-nm-multiplication-table
https://leetcode.com/articles/kth-smallest-number-in-multiplication-table/
Create the multiplication table and sort it, then take the
• Time Complexity: $O(m*n)$ to create the table, and $O(m*n\log(m*n))$ to sort it.
• Space Complexity: $O(m*n)$ to store the table.

• Time Complexity: $O(k * m \log m) = O(m^2 n \log m)$. Our initial heapify operation is $O(m)$. Afterwards, each pop and push is $O(m \log m)$, and our outer loop is $O(k) = O(m*n)$
• Space Complexity: $O(m)$. Our heap is implemented as an array with $m$ elements.
Maintain a heap of the smallest unused element of each row. Then, finding the next element is a pop operation on the heap.
Algorithm
Our heap is going to consist of elements $\text{(val, root)}$, where $\text{val}$ is the next unused value of that row, and $\text{root}$ was the starting value of that row.
We will repeatedly find the next lowest element in the table. To do this, we pop from the heap. Then, if there's a next lowest element in that row, we'll put that element back on the heap.
    public int findKthNumber(int m, int n, int k) {
PriorityQueue<Node> heap = new PriorityQueue<Node>(m,
Comparator.<Node> comparingInt(node -> node.val));

for (int i = 1; i <= m; i++) {
heap.offer(new Node(i, i));
}

Node node = null;
for (int i = 0; i < k; i++) {
node = heap.poll();
int nxt = node.val + node.root;
if (nxt <= node.root * n) {
heap.offer(new Node(nxt, node.root));
}
}
return node.val;
}

class Node {
int val;
int root;
public Node(int v, int r) {
val = v;
root = r;
}
}