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 ❤❤❤❤
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]
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.
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.
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)
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
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])
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.
You are the best coding channel...my addiction with coding is only due to you
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 ❤❤❤❤
2 videos in less than 24 hrs. great dedication even during weekend, Hats off !! and much to learn from you apart from coding
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)
O(n^2)
Very good approach learned so much from this video
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]
we can even get rid of variable `L` and checking the first `if` to be `if R > k`
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.
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.
can you list more techniques like SLIDING WINDOW with advantages for practicing
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)
I understood this better than the actual video 😭😭
HI Neet, please look into contains duplicate III. I'd love to see your walkthrough
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
}
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
Thank you sir.
this screamed nested for loop for me and it worked but i need to work on big o and optimization lol i suck.
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])
Thank you
nums = [1,0,1,1] in this case your solution is wrong
but initially we are not adding any values to the window set so how does this is working ?? please help
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.
i am not able to understand this code logic can anyone explain
Why would the window size be k+1?
Optimised
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.
Hi NEET can make a video on leetcode 2564. Substring XOR Queries?
1263 Minimum Moves to Move a Box to Their Target Location could you pls help to solve this question.
dict_ = {}
for i, n in enumerate(nums):
if n in dict_ and abs(i-dict_[n])
nice,
dont need the abs method, cuz the i is always going be to bigger than whatever is in the dict
This solution is why it was classified as easy imo. Neetcode overengineered I think
@@benji-t7v There the SC is O(k) and here is O(N)
Face reveallllllllllll anytime soon ????
i dont even know how tf would i ever come up with this code
I solved it but still got it wrong so I’m here once again feeling like a dummy
i feel myself so dumb(