Given an integer array of size $n$ containing prime integers, it can form $n(n-1)/2$ fractions, we are required to find the $k$-th smallest prime fraction.
Intuition
We can use a priority queue to store prime integers, then we maintain the priority queue.
Approach1: Brute force
Complexity
Time complexity: iterate the array once and maintain the priority queue. $$O(n^2\log k)$$
Space complexity: the size of the priority queue $$O(k)$$
Code
|
|
Approach2: Simplification
Notice that Approach1 requires iterating over all fractions, can we reduce the time complexity?
The solution is by considering the relative order, if we write a matrix whose element a[i][j]=nums[i]/nums[j]
(i<j
), then we know that a[i][i+1]>...>a[n-1][n]
since the array nums
are increasing. So, the smallest fraction are in a[1][2], ..., a[n-1][n]
. If we take the smallest fraction, and add its successive elements (same column, last row), then we can find the second smallest fraction and so on. This solution requires iterating over $\max(n, k)$ fractions and $O(n)$ spaces.
Complexity
Time complexity: iterate $\max(n, k)$ elements and maintain the priority queue. $$O(\max(n, k)\log n)$$
Space complexity: the size of the priority queue $$O(n)$$
Code
|
|