Python Simple heap solution (266 ms)


  • 0
    S
    This post is deleted!

  • 0

    Gets NameError: global name 'deque' is not defined.


  • 0
    S

    @StefanPochmann thanks. forgot from collections import deque


  • 0

    @slcott Not clear where you write the import. Inside the class or before the class?

    Also, next error is NameError: global name 'heappush' is not defined.


  • 0
    S

    @StefanPochmann please stop asking questions you know the answer to.


  • 0

    @slcott Well while I do know how to fix it, I wanted to see how your actual code did it. Because with the usual way, which is also what your posted code suggested, I doubt it got 266 ms. I also know a way to make it that fast, but that's different from what you made yours look like, and it's an optimization I'd find worth sharing. I was hoping you'd do that.

    Edit: Hmm, I just tried it again. I thought when I tested it yesterday, your solution fixed the normal way (imports above the class) got times around 500 ms. But now I do get times around 250-300ms. I thought yesterday I only got there by importing inside your function, making them fast local variables instead of slower global ones. But now that doesn't seem to make a noticeable difference, and now that I think about that, I don't think it should, as it's not that significant in comparison to the rest of the code. Oh well... in any case, I think it would've been better had you posted your full code. Then I wouldn't have had to make assumptions, and people who don't know how to make it work could still easily play with it.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.