Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

我在leetcode中对MinPriorityQueue实例化,然后使用enquene方法报错 #21

Closed
Heron-Wang opened this issue Apr 19, 2021 · 12 comments

Comments

@Heron-Wang
Copy link

我在leetcode中进行对MinPriorityQueue实例化,然后使用enquene方法报错

const p = new MinPriorityQueue();
p.enquene(3);   //没有这一行是可以打印出p的结构的
console.log(p);

没有 上面的p.enquene(3); 会打印出如下信息:

MinPriorityQueue {
  _getPriority: null,
  _heap: MinHeap { _nodes: [], _leaf: null }
}

使用了p.enquene(3); 报错信息如下

/.nvm/versions/node/v14.8.0/lib/node_modules/@datastructures-js/priority-queue/src/priorityQueue.js:79
      throw new Error('missing priority number or constructor callback');
      ^
Error: missing priority number or constructor callback
    Line 79: Char 13 in priorityQueue.js (MinPriorityQueue.enqueue)
    Line 11: Char 18 in solution.js (removeElement)
    Line 36: Char 19 in solution.js (Object.<anonymous>)
    Line 16: Char 8 in runner.js (Object.runner)
    Line 25: Char 26 in solution.js (Object.<anonymous>)
    Line 1251: Char 30 in loader.js (Module._compile)
    Line 1272: Char 10 in loader.js (Object.Module._extensions..js)
    Line 1100: Char 32 in loader.js (Module.load)
    Line 962: Char 14 in loader.js (Function.Module._load)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)

how to solve this problem?

@eyas-ranjous
Copy link
Member

eyas-ranjous commented Apr 19, 2021

Hi @Heron-Wang

Unfortunately, leetcode is using an older version of the PriorityQueue that does not provide a default value for the priority callback when providing an element without priority.

I have sent leetcode a support ticket asking them to upgrade their environment version of priority-queue to latest. In the meanwhile, you can resolve that by doing this:

provide priority as the same value as the element.

const p = new MinPriorityQueue();
p.enquene(3, 3);  
console.log(p);

Cheers

@Heron-Wang
Copy link
Author

I understand ,thanks a lot

@CrownKira
Copy link

Screenshot 2021-12-31 at 1 19 13 PM

Has leetcode bumped the version?
I'm still getting the same error.

@boredcity
Copy link

having .fromArray static method would also help a lot with boilerplate code...

@eyas-ranjous
Copy link
Member

They did not yet. I dropped a message to leetcode support to upgrade to V6.

@LordMoMA
Copy link

LordMoMA commented Aug 6, 2022

You cannot enqueue an array, and you cannot remove a random element, the priority queue is so lame compared to Heap. And you cannot use Heap in LeetCode. So there is no way to work out a best optimise solution with JavaScript during a technical interview which requires print out.

If you don't believe me, try LeetCode 480 and 502. Trust me, they will drive you crazy, even though you know how to solve it, but you simply could not run it out. It feels like an invisible hand holds your dick from urinating, and you are suffocating.

I wish I choose Python for coding practice, but it's too late.

@eyas-ranjous
Copy link
Member

eyas-ranjous commented Aug 6, 2022

Hey, sorry to hear that. first of all, has leetcode upgraded to latest version, which uses a compare function? If not, I really don't understand why despite sending them several messages.

Also it's not a lame queue. You should know that this whole project started a simple learning project and it scaled out as time progressed. This wasn't built for leetcode, the decided to use at some version. The Heap is not doing anything different, the queue just has a more common interface.

Wha do you mean by "enqueue an array". You can enqueue any element into any queue. And you can convert any array into a queue.

And regarding removing any element randomly from the queue, that would not be the queue interface right? The queue removal is by priority here. Even if I added you a "remove" function, it will be based on dequeue. So you should dequeue elements till reaching the one you want out, dequeue it, and then return the other elements back.

I will reach out again to leetcode to see what version they use.

@eyas-ranjous
Copy link
Member

eyas-ranjous commented Aug 8, 2022

fyi - confirming that I have sent another request to leetcode to urgently upgrade the priority-queue lib to v6.

@crowbardispensingcore
Copy link

crowbardispensingcore commented Jan 18, 2023

It's 2023 and this still doesn't work. Looks like I have to give up js for leetcoding
image
image

@eyas-ranjous
Copy link
Member

They still haven't upgraded it. Sent them couple of times asking to upgrade and explaining in details why. Don't know what else to do.

@LordMoMA
Copy link

LordMoMA commented Jan 18, 2023 via email

@harizjet
Copy link

harizjet commented May 16, 2023

We can customize the compare methods inside the heap: so it becomes something like this
new MaxPriorityQueue({compare: (x, y) => {< your custom compare >}
this would allow you to enqueue any kind of ds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants