website/content/ChapterFour/0700~0799/0786.K-th-Smallest-Prime-Fraction.md
A sorted list A contains 1, plus some number of primes. Then, for every p < q in the list, we consider the fraction p/q.
What is the K-th smallest fraction considered? Return your answer as an array of ints, where answer[0] = p and answer[1] = q.
Examples:
Input: A = [1, 2, 3, 5], K = 3
Output: [2, 5]
Explanation:
The fractions to be considered in sorted order are:
1/5, 1/3, 2/5, 1/2, 3/5, 2/3.
The third fraction is 2/5.
Input: A = [1, 7], K = 1
Output: [1, 7]
Note:
A will have length between 2 and 2000.A[i] will be between 1 and 30000.K will be between 1 and A.length * (A.length - 1) / 2.一个已排序好的表 A,其包含 1 和其他一些素数. 当列表中的每一个 p<q 时,我们可以构造一个分数 p/q 。
那么第 k 个最小的分数是多少呢? 以整数数组的形式返回你的答案, 这里 answer[0] = p 且 answer[1] = q.
注意:
package leetcode
import (
"sort"
)
// 解法一 二分搜索
func kthSmallestPrimeFraction(A []int, K int) []int {
low, high, n := 0.0, 1.0, len(A)
// 因为是在小数内使用二分查找,无法像在整数范围内那样通过 mid+1 和边界判断来终止循环
// 所以此处根据 count 来结束循环
for {
mid, count, p, q, j := (high+low)/2.0, 0, 0, 1, 0
for i := 0; i < n; i++ {
for j < n && float64(A[i]) > float64(mid)*float64(A[j]) {
j++
}
count += n - j
if j < n && q*A[i] > p*A[j] {
p = A[i]
q = A[j]
}
}
if count == K {
return []int{p, q}
} else if count < K {
low = mid
} else {
high = mid
}
}
}
// 解法二 暴力解法,时间复杂度 O(n^2)
func kthSmallestPrimeFraction1(A []int, K int) []int {
if len(A) == 0 || (len(A)*(len(A)-1))/2 < K {
return []int{}
}
fractions := []Fraction{}
for i := 0; i < len(A); i++ {
for j := i + 1; j < len(A); j++ {
fractions = append(fractions, Fraction{molecule: A[i], denominator: A[j]})
}
}
sort.Sort(SortByFraction(fractions))
return []int{fractions[K-1].molecule, fractions[K-1].denominator}
}
// Fraction define
type Fraction struct {
molecule int
denominator int
}
// SortByFraction define
type SortByFraction []Fraction
func (a SortByFraction) Len() int { return len(a) }
func (a SortByFraction) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a SortByFraction) Less(i, j int) bool {
return a[i].molecule*a[j].denominator < a[j].molecule*a[i].denominator
}