Last active
May 10, 2024 17:01
-
-
Save tatsuyax25/3db398b50ed3f2948451c15d4148fafc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[]} arr | |
* @param {number} k | |
* @return {number[]} | |
*/ | |
// Define the function that takes an array and a number k as input | |
var kthSmallestPrimeFraction = function(arr, k) { | |
// Initialize a new max priority queue | |
let newarr = new MaxPriorityQueue(); | |
// Loop through each element in the array | |
for(let i = 0; i < arr.length; i++){ | |
// For each element, loop through the rest of the array | |
for(let j = i + 1; j < arr.length; j++){ | |
// Enqueue the fraction (arr[i] / arr[j]) into the priority queue | |
// The key is a string of arr[i] and arr[j], and the value is the fraction | |
newarr.enqueue(`${arr[i]} ${arr[j]}`, arr[i] / arr[j]); | |
// If the size of the priority queue is greater than k | |
if(newarr.size() > k){ | |
// Dequeue the max element from the priority queue | |
newarr.dequeue(); | |
} | |
} | |
} | |
// Dequeue the max element from the priority queue | |
// Split the key into two numbers and return them as an array | |
return newarr.dequeue().element.split(" ").map(v => Number(v)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment