JS运行时中的MaxPriorityQueue从数组创建不是一个函数

3
根据https://support.leetcode.com/hc/en-us/articles/360011833974-What-are-the-environments-for-the-programming-languages-中不同语言支持的运行时,我使用https://github.com/datastructures-js/priority-queue#fromarray,因为这是一个受支持的库函数。
然而,当我使用
const maxHeap = MaxPriorityQueue.fromArray(nums);

我遇到了一个错误。
Line 11 in solution.js
  const maxHeap = MaxPriorityQueue.fromArray(nums);
                                   ^
TypeError: MaxPriorityQueue.fromArray is not a function
    Line 11: Char 36 in solution.js (findKthLargest)
    Line 34: Char 19 in solution.js (Object.<anonymous>)
    Line 16: Char 8 in runner.js (Object.runner)
    Line 23: Char 26 in solution.js (Object.<anonymous>)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

这符合提供的示例https://github.com/datastructures-js/priority-queue#js-3


你已经采取了什么措施来调查这个问题?你有使用浏览器调试工具来查看MaxPriorityQueue对象吗? - Pointy
这是在Leetcode的JS运行时。我尝试了多种选项,考虑到文档可能不正确。将其作为函数调用或使用构造函数也没有帮助。它唯一的替代方法是逐个入队所有项目,这将导致O(nlog(n))的时间复杂度,而不是O(n)。 - Omkar Kulkarni
好的,但那并没有回答我的问题。 - Pointy
提到了我迄今为止所做的事情。没有在浏览器调试器中尝试过这个。 - Omkar Kulkarni
你是在哪里尝试它?在浏览器中还是在Node中?为什么不检查调试器或者至少添加一些 console.log() 语句呢? - Pointy
如评论中所述,它是在LeetCode JS运行时中。console.log只是表明它扩展了PriorityQueue类。对它使用Object.keys只会显示['_priority', '_heap']。当然,在寻求帮助之前,我肯定已经尝试过console.log。 - Omkar Kulkarni
1个回答

3
我觉得现在回答有些晚了,但是我还是会回答的,因为我以前也遇到过同样的问题。
问题在于Leetcode使用的是4.1版本的库,在这个版本中,fromArray方法还不存在。
可悲的是,据我所查,没有任何替代该方法的方法。
文档:https://www.npmjs.com/package/@datastructures-js/priority-queue/v/4.1.0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接