返回介绍

solution / 0700-0799 / 0786.K-th Smallest Prime Fraction / README_EN

发布于 2024-06-17 01:03:34 字数 4977 浏览 0 评论 0 收藏 0

786. K-th Smallest Prime Fraction

中文文档

Description

You are given a sorted integer array arr containing 1 and prime numbers, where all the integers of arr are unique. You are also given an integer k.

For every i and j where 0 <= i < j < arr.length, we consider the fraction arr[i] / arr[j].

Return _the_ kth _smallest fraction considered_. Return your answer as an array of integers of size 2, where answer[0] == arr[i] and answer[1] == arr[j].

 

Example 1:

Input: arr = [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, and 2/3.
The third fraction is 2/5.

Example 2:

Input: arr = [1,7], k = 1
Output: [1,7]

 

Constraints:

  • 2 <= arr.length <= 1000
  • 1 <= arr[i] <= 3 * 104
  • arr[0] == 1
  • arr[i] is a prime number for i > 0.
  • All the numbers of arr are unique and sorted in strictly increasing order.
  • 1 <= k <= arr.length * (arr.length - 1) / 2

 

Follow up: Can you solve the problem with better than O(n<sup>2</sup>) complexity?

Solutions

Solution 1

class Solution:
  def kthSmallestPrimeFraction(self, arr: List[int], k: int) -> List[int]:
    h = [(1 / y, 0, j + 1) for j, y in enumerate(arr[1:])]
    heapify(h)
    for _ in range(k - 1):
      _, i, j = heappop(h)
      if i + 1 < j:
        heappush(h, (arr[i + 1] / arr[j], i + 1, j))
    return [arr[h[0][1]], arr[h[0][2]]]
class Solution {
  public int[] kthSmallestPrimeFraction(int[] arr, int k) {
    int n = arr.length;
    Queue<Frac> pq = new PriorityQueue<>();
    for (int i = 1; i < n; i++) {
      pq.offer(new Frac(1, arr[i], 0, i));
    }
    for (int i = 1; i < k; i++) {
      Frac f = pq.poll();
      if (f.i + 1 < f.j) {
        pq.offer(new Frac(arr[f.i + 1], arr[f.j], f.i + 1, f.j));
      }
    }
    Frac f = pq.peek();
    return new int[] {f.x, f.y};
  }

  static class Frac implements Comparable {
    int x, y, i, j;

    public Frac(int x, int y, int i, int j) {
      this.x = x;
      this.y = y;
      this.i = i;
      this.j = j;
    }

    @Override
    public int compareTo(Object o) {
      return x * ((Frac) o).y - ((Frac) o).x * y;
    }
  }
}
class Solution {
public:
  vector<int> kthSmallestPrimeFraction(vector<int>& arr, int k) {
    using pii = pair<int, int>;
    int n = arr.size();
    auto cmp = [&](const pii& a, const pii& b) {
      return arr[a.first] * arr[b.second] > arr[b.first] * arr[a.second];
    };
    priority_queue<pii, vector<pii>, decltype(cmp)> pq(cmp);
    for (int i = 1; i < n; ++i) {
      pq.push({0, i});
    }
    for (int i = 1; i < k; ++i) {
      pii f = pq.top();
      pq.pop();
      if (f.first + 1 < f.second) {
        pq.push({f.first + 1, f.second});
      }
    }
    return {arr[pq.top().first], arr[pq.top().second]};
  }
};
type frac struct{ x, y, i, j int }
type hp []frac

func (a hp) Len() int       { return len(a) }
func (a hp) Swap(i, j int)    { a[i], a[j] = a[j], a[i] }
func (a hp) Less(i, j int) bool { return a[i].x*a[j].y < a[j].x*a[i].y }
func (a *hp) Push(x any)    { *a = append(*a, x.(frac)) }
func (a *hp) Pop() any      { l := len(*a); tmp := (*a)[l-1]; *a = (*a)[:l-1]; return tmp }

func kthSmallestPrimeFraction(arr []int, k int) []int {
  n := len(arr)
  h := make(hp, 0, n-1)
  for i := 1; i < n; i++ {
    h = append(h, frac{1, arr[i], 0, i})
  }
  heap.Init(&h)
  for i := 1; i < k; i++ {
    f := heap.Pop(&h).(frac)
    if f.i+1 < f.j {
      heap.Push(&h, frac{arr[f.i+1], arr[f.j], f.i + 1, f.j})
    }
  }
  return []int{h[0].x, h[0].y}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文