diff --git a/content/posts/leetcode.md b/content/posts/leetcode.md index 702e78f..1377c3d 100644 --- a/content/posts/leetcode.md +++ b/content/posts/leetcode.md @@ -5348,3 +5348,81 @@ func maximumHappinessSum(happiness []int, k int) int64 { return int64(result) } ``` + +## day73 2024-05-10 + +### 786. K-th Smallest Prime Fraction + +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]. + +![0510L1MMRZ5XYJzh](https://testingcf.jsdelivr.net/gh/game-loader/picbase@master/uPic/0510L1MMRZ5XYJzh.png) + +### 题解 + +保存每个分数的值, 和对应的分子分母, 对对应的分数值排序, 取第k小的即可 + +### 代码 + +```go + +func kthSmallestPrimeFraction(arr []int, k int) []int { + type point struct{ + value float32 + num []int + } + points := []point{} + length := len(arr) + i := 0 + for index, value := range arr{ + float32val := float32(value) + for i=index+1;i mid { + j++ + } + count += n - j + if j < n && float64(arr[i])/float64(arr[j]) > float64(maxFraction[0])/float64(maxFraction[1]) { + maxFraction = [2]int{arr[i], arr[j]} + } + } + + if count == k { + return maxFraction[:] + } else if count < k { + left = mid + } else { + right = mid + } + } + + return result +} +```