Design Hashmap - Leetcode 706 - Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 31 ม.ค. 2025

ความคิดเห็น • 56

  • @mohamedsalama2743
    @mohamedsalama2743 11 หลายเดือนก่อน +3

    you can just use a frequency array with the total size and fill all the numbers with -1 and then just update the values with put/remove, the only drawback is that this will have a space of O(N), but it's much less code and it's like a Hashmap anyway

  • @smartsoothing776
    @smartsoothing776 ปีที่แล้ว +74

    I think this is not an easy problem!!

    • @rasuldanierov1353
      @rasuldanierov1353 ปีที่แล้ว

      easy )

    • @sameensolkar6193
      @sameensolkar6193 11 หลายเดือนก่อน +4

      yes, only if you don't know how hashtable works

    • @felipeklahn988
      @felipeklahn988 6 หลายเดือนก่อน +1

      It was because on the problem it didn't actually required chaining, if you see on the put method the problem described it asked to just update the value if the key already exists. But the way NeetCode implemented he used chaining and that what's made it harder, but it's a great explanation

    • @neerajbhale
      @neerajbhale 5 หลายเดือนก่อน

      ​@@felipeklahn988 chaining is being done for the index itself not the value

    • @ARkhan-xw8ud
      @ARkhan-xw8ud 5 หลายเดือนก่อน

      me too

  • @AkmalNodirov-vt9gw
    @AkmalNodirov-vt9gw 6 หลายเดือนก่อน +1

    I just used an array before, as the key and value is also integer. That is correct, we should use hashed indexes as general has maps intented to store any kind of type. But in our case it does not necessary, we can just get 1000 rooms of array and store every element as a value. This is just my opinion. By the way, untill watching this video I did not comprehend how actually hash map works. Thanks Navdeep

  • @IK-xk7ex
    @IK-xk7ex ปีที่แล้ว +9

    I've stared to love DUMMY NODES!

  • @business_central
    @business_central 19 วันที่ผ่านมา +2

    I am struggling to understand why we chose specifically 1000 for the size. I know it works, but why exactly? Would appreciate any help 🙏

  • @ShivaGolla-t3s
    @ShivaGolla-t3s ปีที่แล้ว +2

    Why is the first node's key and value equal to -1 in the ListNode?
    Where are we defining it to be -1?

    • @cinimodmil
      @cinimodmil ปีที่แล้ว

      we're simply giving it a default value as the 'dummy' node to avoid edge cases where self.map[self.hash(key)] is None. also, the constraint shows that key and value are in this range: 0

  • @mlevvy96
    @mlevvy96 10 หลายเดือนก่อน +4

    Do we really need to check `while *cur* and cur.next` for remove operation? Can't we just use `while cur.next' ? I think it's redundant because if there is cur.next then `cur` is definitely not None and we modify the 'next' node anyway for the actual removal process

    • @ps_v.2.3.20
      @ps_v.2.3.20 6 หลายเดือนก่อน

      Same thought! Just try with cur.next and it works cool

    • @prakul697
      @prakul697 3 หลายเดือนก่อน +1

      Cur has to exist before you check cur.next none type can't have next.

  • @yizhang9089
    @yizhang9089 ปีที่แล้ว +2

    So we don't need to initialize a dummy node?

    • @stevemiller123
      @stevemiller123 5 หลายเดือนก่อน +1

      He made the dummy nodes when he did:
      self.hashmap = [LinkNode() for _ in range(MAX)]

  • @shaco6630
    @shaco6630 ปีที่แล้ว +8

    I thought the question said that there would be at most 10,000 operations done on the map ?

    • @YouProductions1000
      @YouProductions1000 ปีที่แล้ว

      Yeah it does. The solution is still fine though since keys > 1000 get modded to an index between 0-999, and it's better for memory compared to initializing 10,000 nodes (maybe not for time complexity, performing get operations)

  • @aaronye4857
    @aaronye4857 ปีที่แล้ว +3

    Why is the key and value equal to -1 when init the ListNode?

  • @krateskim4169
    @krateskim4169 ปีที่แล้ว +2

    you are great

  • @edis671games
    @edis671games ปีที่แล้ว +19

    anyone here from october 4?

  • @pikugoud1358
    @pikugoud1358 ปีที่แล้ว

    you should arrays and its indices instead of map

  • @19.sairoopesh10
    @19.sairoopesh10 7 หลายเดือนก่อน

    was stuck on that edge case

  • @aamrahmunawwar5033
    @aamrahmunawwar5033 ปีที่แล้ว +1

    why cant me just use an array instead of an array of ListNodes?

    • @utkarshmaurya6877
      @utkarshmaurya6877 ปีที่แล้ว +5

      Because insertion and deletion won't be constant time in an array, you will have to shift values. That won't be a problem if you use a linked list.

  • @tanish5662
    @tanish5662 ปีที่แล้ว +2

    still not sure
    why you have ussed 1000

  • @jeminpatel3273
    @jeminpatel3273 ปีที่แล้ว

    why can't we use list?

  • @selfhelpguy5589
    @selfhelpguy5589 8 หลายเดือนก่อน +2

    Why 1000?

  • @stevemiller123
    @stevemiller123 5 หลายเดือนก่อน

    Why do we want a value between 0-999?

  • @prajwalrawat333
    @prajwalrawat333 ปีที่แล้ว +1

    Hey, have been watching your videos and following.
    I want to be good in DSA to crack interviews, what path should i follow?

  • @shashankhegde8365
    @shashankhegde8365 ปีที่แล้ว

    Why can't we just use a dictinary rather than an array?

    • @coolkaw4497
      @coolkaw4497 ปีที่แล้ว +3

      because isnt a dictionary essentially a hashmap already so there would be no point 😂

  • @aditijain2448
    @aditijain2448 ปีที่แล้ว

    why use linked list when we can use simple list

    • @VaishnaviMamilla
      @VaishnaviMamilla ปีที่แล้ว +1

      I think remove in a list takes O(n) TC where as in LL it takes O(1).That may be the reason

  • @flamendless
    @flamendless ปีที่แล้ว

    Why not [List()] * 1000?

    • @NeetCodeIO
      @NeetCodeIO  ปีที่แล้ว +1

      Yeah, that would've been more concise

    • @felixsla21
      @felixsla21 ปีที่แล้ว +4

      But wouldn't this copy the same ListNode reference 1000 times?

    • @frigginPAN
      @frigginPAN ปีที่แล้ว +5

      @@felixsla21 yeah youre right. I also thought [List()]*1000 would make 1000 objects but it wont. It just make one object and then stores its reference in all indexes

    • @codedoctor3265
      @codedoctor3265 6 หลายเดือนก่อน

      @NeetCodeIO i think you should initialize with 10000 instead 1000 since question say there 10**4 calls

  • @omaryahia
    @omaryahia 7 หลายเดือนก่อน

    ،amazing

  • @giz2001
    @giz2001 2 หลายเดือนก่อน +2

    Im so cooked

  • @Yougottacryforthis
    @Yougottacryforthis ปีที่แล้ว

    kinda awful hashmap but hey it meets the api requirements i guess. sad to see no dyanmic array, solving collisions with list and not a tree and so on