Contains Duplicate II - Leetcode 219 - Python

Поділитися
Вставка
  • Опубліковано 16 гру 2024

КОМЕНТАРІ • 37

  • @shashanksingh4510
    @shashanksingh4510 Рік тому +11

    You are the best coding channel...my addiction with coding is only due to you

  • @heenakhan295
    @heenakhan295 Рік тому +7

    I used to hate leetcode it have always been difficult for me to understand question of Leetcode, But since I came across your content It became easy for me to understand questions and approaches even though I code in java I always turn to your channel when I get stuck on a problem . Thank you so much ❤❤❤❤

  • @aishwariyaaish8305
    @aishwariyaaish8305 Рік тому +9

    2 videos in less than 24 hrs. great dedication even during weekend, Hats off !! and much to learn from you apart from coding

  • @juderoosevelt2817
    @juderoosevelt2817 4 місяці тому +1

    class Solution(object):
    def containsNearbyDuplicate(self, nums, k):
    for i in range (len(nums)):
    for j in range (i):
    if(nums[i]==nums[j] and abs(i-j)

  • @immortalphoenix8886
    @immortalphoenix8886 Рік тому +2

    Very good approach learned so much from this video

  • @officialaalamjotsingh9465
    @officialaalamjotsingh9465 11 місяців тому +6

    this is my aproach to the problem
    def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
    num_dict = {}
    for i, num in enumerate(nums):
    if num in num_dict and i - num_dict[num]

  • @TrevoZnaniy
    @TrevoZnaniy 10 місяців тому

    we can even get rid of variable `L` and checking the first `if` to be `if R > k`

    • @stylisgames
      @stylisgames 4 місяці тому

      Hmm I don't think so. R - L is the size of the window because L may be shifted to the right at any point. You can't calculate the size of the window without L.

  • @IbrahimLaeeq-v2g
    @IbrahimLaeeq-v2g 5 місяців тому

    another thing to note here is that we are using a "set" becuase we can check the existance of values i.e if value in window in O(1) as oppossed to a list where we would have to do it in O(n) time the other property of set which stores only unique values isn't really used here so that's a note-worthy point.

  • @horizon6285
    @horizon6285 Рік тому +3

    can you list more techniques like SLIDING WINDOW with advantages for practicing

  • @sykarius01
    @sykarius01 Рік тому +2

    hashmap approach
    class Solution:
    def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
    hmap={}
    for i in range(len(nums)):
    if nums[i] in hmap:
    if abs(hmap[nums[i]]-i)

    • @juderoosevelt2817
      @juderoosevelt2817 4 місяці тому +1

      I understood this better than the actual video 😭😭

  • @Tech-sl2hp
    @Tech-sl2hp 4 місяці тому

    HI Neet, please look into contains duplicate III. I'd love to see your walkthrough

  • @2NormalHuman
    @2NormalHuman 5 днів тому

    interesting, i personally didn't use set / dict in my solution:
    fun containsNearbyDuplicate(nums: IntArray, k: Int): Boolean {
    if(nums.size = k || j == nums.lastIndex && i != nums.lastIndex - 1-> {
    i++
    j = i+1
    }
    else -> {
    j++
    }
    }
    }
    return false
    }

  • @binfos7434
    @binfos7434 Рік тому

    Maybe it's better to use hashmap?
    from typing import List
    class Solution:
    def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
    temp_hash = {}
    l = 0
    temp_hash[nums[l]] = 1

    for r, val in enumerate(nums[1:], start=1):
    if (r - l) > k:
    temp_hash[nums[l]] -= 1
    l += 1
    temp_hash[val] = temp_hash.get(val, 0) + 1
    if temp_hash[val] > 1:
    return True
    else:
    return False

  • @servantofthelord8147
    @servantofthelord8147 Рік тому +1

    Thank you sir.

  • @SLowe-xi3fq
    @SLowe-xi3fq 10 місяців тому

    this screamed nested for loop for me and it worked but i need to work on big o and optimization lol i suck.

  • @nikhilg251
    @nikhilg251 4 місяці тому

    class Solution(object):
    def containsNearbyDuplicate(self, nums, k):
    """
    :type nums: List[int]
    :type k: int
    :rtype: bool
    """
    dic = {}
    for ind,i in enumerate(nums):
    if i in dic and abs(ind-dic[i])

  • @krateskim4169
    @krateskim4169 Рік тому +1

    Thank you

  • @vidhushekhar8144
    @vidhushekhar8144 6 місяців тому +2

    nums = [1,0,1,1] in this case your solution is wrong

  • @sankaranarayanankm7049
    @sankaranarayanankm7049 Рік тому

    but initially we are not adding any values to the window set so how does this is working ?? please help

    • @ChrisCox-wv7oo
      @ChrisCox-wv7oo Рік тому

      All values are added at line 12. You don't need to set an initial value for the set. The for loop will add the first element the first iteration.

  • @KishoreRE
    @KishoreRE 8 місяців тому +2

    i am not able to understand this code logic can anyone explain

  • @maahasgharali571
    @maahasgharali571 Рік тому +1

    Why would the window size be k+1?

    • @user-le6ts6ci7h
      @user-le6ts6ci7h Рік тому

      Optimised

    • @leeroymlg4692
      @leeroymlg4692 Рік тому +1

      because k is not how many elements are in the window, but how far apart they are. So if you have k = 1, you need a window size of 2 because the 2 elements are 1 index apart.

  • @cosepeter2197
    @cosepeter2197 Рік тому

    Hi NEET can make a video on leetcode 2564. Substring XOR Queries?

  • @aishwariyaaish8305
    @aishwariyaaish8305 Рік тому

    1263 Minimum Moves to Move a Box to Their Target Location could you pls help to solve this question.

  • @sergiofranklin8809
    @sergiofranklin8809 Рік тому +3

    dict_ = {}
    for i, n in enumerate(nums):
    if n in dict_ and abs(i-dict_[n])

    • @kumarc4853
      @kumarc4853 7 місяців тому +1

      nice,
      dont need the abs method, cuz the i is always going be to bigger than whatever is in the dict

    • @benji-t7v
      @benji-t7v Місяць тому

      This solution is why it was classified as easy imo. Neetcode overengineered I think

    • @LiadMotorin
      @LiadMotorin 14 днів тому

      @@benji-t7v There the SC is O(k) and here is O(N)

  • @visase2036
    @visase2036 Рік тому +11

    Face reveallllllllllll anytime soon ????

  • @kimashiidan
    @kimashiidan 6 місяців тому

    i dont even know how tf would i ever come up with this code

  • @wantedunknowngaming0504
    @wantedunknowngaming0504 Рік тому

    I solved it but still got it wrong so I’m here once again feeling like a dummy

  • @bossmusa9075
    @bossmusa9075 Рік тому +1

    i feel myself so dumb(