zoukankan      html  css  js  c++  java
  • [Swift]LeetCode786. 第 K 个最小的素数分数 | K-th Smallest Prime Fraction

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址: https://www.cnblogs.com/strengthen/p/10545344.html 
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    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.
    • Each 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.

    示例:
    输入: A = [1, 2, 3, 5], K = 3
    输出: [2, 5]
    解释:
    已构造好的分数,排序后如下所示:
    1/5, 1/3, 2/5, 1/2, 3/5, 2/3.
    很明显第三个最小的分数是 2/5.
    
    输入: A = [1, 7], K = 1
    输出: [1, 7]
    

    注意:

    • A 的取值范围在 2 — 2000.
    • 每个 A[i] 的值在 1 —30000.
    • K 取值范围为 1 —A.length * (A.length - 1) / 2

    Runtime: 64 ms
    Memory Usage: 19.1 MB
     1 class Solution {
     2     func kthSmallestPrimeFraction(_ A: [Int], _ K: Int) -> [Int] {
     3         var left:Double = 0
     4         var right:Double = 1.0
     5         var p:Int = 0
     6         var q:Int = 1
     7         var cnt:Int = 0
     8         var n:Int = A.count
     9         while(true)
    10         {
    11             var mid:Double = left + (right - left) / 2.0
    12             cnt = 0
    13             p = 0
    14             var j:Int = 0
    15             for i in 0..<n
    16             {
    17                 while(j < n && Double(A[i]) > mid * Double(A[j]))
    18                 {
    19                     j += 1
    20                 }
    21                 cnt += n - j
    22                 if j < n && p * A[j] < q * A[i]
    23                 {
    24                     p = A[i]
    25                     q = A[j]
    26                 }                
    27             }
    28             if cnt == K {return [p,q]}
    29             else if cnt < K {left = mid}
    30             else {right = mid}
    31         }
    32     }
    33 }
  • 相关阅读:
    Learn Prolog Now 翻译
    Learn Prolog Now 翻译
    Learn Prolog Now 翻译
    Learn Prolog Now 翻译
    Learn Prolog Now 翻译
    Learn Prolog Now 翻译
    Learn Prolog Now 翻译
    Learn Prolog Now 翻译
    Learn Prolog Now 翻译
    Learn Prolog Now 翻译
  • 原文地址:https://www.cnblogs.com/strengthen/p/10545344.html
Copyright © 2011-2022 走看看