|
| 1 | +/** |
| 2 | +* A sorted list A contains 1, plus some number of primes. Then, for every p < q |
| 3 | +* in the list, we consider the fraction p/q. |
| 4 | +* |
| 5 | +* What is the K-th smallest fraction considered? Return your answer as an |
| 6 | +* array of ints, where answer[0] = p and answer[1] = q. |
| 7 | +* |
| 8 | +* Examples: |
| 9 | +* Input: A = [1, 2, 3, 5], K = 3 |
| 10 | +* Output: [2, 5] |
| 11 | +* Explanation: |
| 12 | +* The fractions to be considered in sorted order are: |
| 13 | +* 1/5, 1/3, 2/5, 1/2, 3/5, 2/3. |
| 14 | +* The third fraction is 2/5. |
| 15 | +* |
| 16 | +* Input: A = [1, 7], K = 1 |
| 17 | +* Output: [1, 7] |
| 18 | +* |
| 19 | +* Note: |
| 20 | +* A will have length between 2 and 2000. |
| 21 | +* Each A[i] will be between 1 and 30000. |
| 22 | +* K will be between 1 and A.length * (A.length - 1) / 2. |
| 23 | +*/ |
| 24 | + |
| 25 | +/** |
| 26 | +* https://leetcode.com/problems/k-th-smallest-prime-fraction/discuss/115819/Summary-of-solutions-for-problems-%22reducible%22-to-LeetCode-378 |
| 27 | +*/ |
| 28 | +public class KthSmallestPrimeFraction786 { |
| 29 | +public int[] kthSmallestPrimeFraction(int[] A, int K) { |
| 30 | +Comparator<int[]> comp = (int[] a, int[] b) -> { |
| 31 | +return Double.compare(A[a[0]] * 1.0 / A[a[1]], A[b[0]] * 1.0 / A[b[1]]); |
| 32 | +}; |
| 33 | +PriorityQueue<int[]> q = new PriorityQueue<>(comp); |
| 34 | +int len = A.length; |
| 35 | +q.add(new int[]{0, len-1}); |
| 36 | +boolean[][] mark = new boolean[len][len]; |
| 37 | +mark[0][len-1] = true; |
| 38 | +int c = 0; |
| 39 | +while (c + 1 < K) { |
| 40 | +int[] tmp = q.remove(); |
| 41 | +c++; |
| 42 | +int i = tmp[0]; |
| 43 | +int j = tmp[1]; |
| 44 | +if (i+1 < len && i+1 < j && !mark[i+1][j]) { |
| 45 | +q.add(new int[]{i+1, j}); |
| 46 | +mark[i+1][j] = true; |
| 47 | +} |
| 48 | +if (j-1 >= 0 && i < j-1 && !mark[i][j-1]) { |
| 49 | +q.add(new int[]{i, j-1}); |
| 50 | +mark[i][j-1] = true; |
| 51 | +} |
| 52 | +if (i+1 < len && j-1 >= 0 && i+1 < j-1 && !mark[i+1][j-1]) { |
| 53 | +q.add(new int[]{i+1, j-1}); |
| 54 | +mark[i+1][j-1] = true; |
| 55 | +} |
| 56 | +} |
| 57 | +int[] tmp = q.peek(); |
| 58 | +return new int[]{A[tmp[0]], A[tmp[1]]}; |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +/** |
| 63 | +* https://leetcode.com/problems/k-th-smallest-prime-fraction/discuss/115486/Java-AC-O(max(nk)-*-logn)-Short-Easy-PriorityQueue |
| 64 | +*/ |
| 65 | +public int[] kthSmallestPrimeFraction2(int[] a, int k) { |
| 66 | +int n = a.length; |
| 67 | +// 0: numerator idx, 1: denominator idx |
| 68 | +PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>() { |
| 69 | +@Override |
| 70 | +public int compare(int[] o1, int[] o2) { |
| 71 | +int s1 = a[o1[0]] * a[o2[1]]; |
| 72 | +int s2 = a[o2[0]] * a[o1[1]]; |
| 73 | +return s1 - s2; |
| 74 | +} |
| 75 | +}); |
| 76 | +for (int i = 0; i < n-1; i++) { |
| 77 | +pq.add(new int[]{i, n-1}); |
| 78 | +} |
| 79 | +for (int i = 0; i < k-1; i++) { |
| 80 | +int[] pop = pq.remove(); |
| 81 | +int ni = pop[0]; |
| 82 | +int di = pop[1]; |
| 83 | +if (pop[1] - 1 > pop[0]) { |
| 84 | +pop[1]--; |
| 85 | +pq.add(pop); |
| 86 | +} |
| 87 | +} |
| 88 | + |
| 89 | +int[] peek = pq.peek(); |
| 90 | +return new int[]{a[peek[0]], a[peek[1]]}; |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | +* https://leetcode.com/problems/k-th-smallest-prime-fraction/discuss/116107/Java-Better-than-O(NlogN)-it-can-be-O(KlogK) |
| 95 | +*/ |
| 96 | +public int[] kthSmallestPrimeFraction3(int[] A, int K) { |
| 97 | +PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> A[a[0]] * A[b[1]] - A[a[1]] * A[b[0]]); |
| 98 | +pq.offer(new int[]{0, A.length - 1}); |
| 99 | +while (K > 1 && !pq.isEmpty()) { |
| 100 | +int[] cur = pq.poll(); |
| 101 | +if (cur[1] == A.length - 1 && cur[0] + 1 < cur[1]) { |
| 102 | +pq.offer(new int[]{cur[0] + 1, cur[1]}); |
| 103 | +} |
| 104 | +if (cur[0] < cur[1] - 1) { |
| 105 | +pq.offer(new int[]{cur[0], cur[1] - 1}); |
| 106 | +} |
| 107 | +K--; |
| 108 | +} |
| 109 | +if (pq.isEmpty()) { |
| 110 | +throw new RuntimeException("invalid input."); |
| 111 | +} |
| 112 | +return new int[]{A[pq.peek()[0]], A[pq.peek()[1]]}; |
| 113 | +} |
| 114 | +} |
0 commit comments