Approach #1: Brute Force [Memory Limit Exceeded]
Intuition and Algorithm
Create the multiplication table and sort it, then take the element.
Time Complexity: to create the table, and to sort it.
Space Complexity: to store the table.
Approach #2: Next Heap [Time Limit Exceeded]
Maintain a heap of the smallest unused element of each row. Then, finding the next element is a pop operation on the heap.
heap is going to consist of elements , where is the next unused value of that row, and 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.
Time Complexity: . Our initial heapify operation is . Afterwards, each pop and push is , and our outer loop is
Space Complexity: . Our heap is implemented as an array with elements.
Approach #3: Binary Search [Accepted]
As and are up to , linear solutions will not work. This motivates solutions with complexity, such as binary search.
Let's do the binary search for the answer .
enough(x) is true if and only if there are or more values in the multiplication table that are less than or equal to . Colloquially,
enough describes whether is large enough to be the value in the multiplication table.
Then (for our answer ), whenever ,
True; and whenever ,
In our binary search, our loop invariant is
enough(hi) = True. At the beginning,
enough(m*n) = True, and whenever
hi is set, it is set to a value that is "enough" (
enough(mi) = True). That means
hi will be the lowest such value at the end of our binary search.
This leaves us with the task of counting how many values are less than or equal to . For each of rows, the row looks like . The largest possible that could appear is . However, if is really big, then perhaps , so in total there are values in that row that are less than or equal to .
After we have the count of how many values in the table are less than or equal to , by the definition of
enough(x), we want to know if that count is greater than or equal to .
Time Complexity: . Our binary search divides the interval into half at each step. At each step, we call
enoughwhich requires time.
Space Complexity: . We only keep integers in memory during our intermediate calculations.
Analysis written by: @awice