leetcode 786. K-th Smallest Prime Fraction
题目描述
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
.
Note:
A
will have length between2
and2000
.- Each
A[i]
will be between1
and30000
. K
will be between1
andA.length * (A.length - 1) / 2
.
Difficulty: hard
786. K-th Smallest Prime Fraction
中文描述
给你一个包含1在内的,排好序的素数列表,找出第K
小的分数p/q
,p < q
的[p, q]
输入格式
输入一个列表A
,列表素数的集合。K
表示需要第k
小的。
Examples:
Input: A = [1, 2, 3, 5], K = 3
Output: [2, 5]
解释:
由A组成的分数排序:1/5, 1/3, 2/5, 1/2, 3/5, 2/3.
第3小的是 2/5.A = [1, 7], K = 1
Output: [1, 7]
解释:
由A组成的分数排序:1/7.
第1小的是 1/7.
解答思路
解法一:运用heapq
1.把A的元素按顺序记为 p1,p2,p3,⋅⋅⋅,pn (A中有 n 个元素)。
2.heapq先添加
p1pn,p2pn,