Contains Duplicate II - Leetcode 219 - Python
Key Takeaways
The video solves the LeetCode problem 'Contains Duplicate II' using a sliding window technique and a hash set in Python, with a time complexity of O(n) and a memory complexity of O(K).
Full Transcript
hey everyone welcome back and let's write some more neat code today so today let's solve the problem contains duplicate 2 we're given an integer array of nums and an integer K we want to return true if there are two distinct indices I and J such that the two values at those indices are equal and the absolute value difference between those indices is less than or equal to K now the most important thing to recognize here is that by this statement they mean that the size of the window between the two elements is less than or equal to K so take the first example we have 1 2 3 1 clearly we do have some duplicate values here 1 and 1 they are at different indices and the question is is the size of the window less than or equal to K well not exactly actually that's another Edge case that you have to keep track of when we have I and J pointing at the same value here what's the size of this window well it's one there's just a single element but what's the absolute value of the difference between those two indices 0us 0 that's going to equal 0 so this equation is off by one now what about this window from zero all the way to three it's of course of size four does it satisfy the absolute value well we take 3 - 0 that gives us 3 or we could have done the opposite and gotten the absolute abute value of 0 - 3 it would have equal 3 in the end anyway is this value less than or equal to K yes it is so this is a valid window so basically the problem is asking us can we find a valid window of size k + 1 such that there are duplicate values in that window well what's the easiest way to solve this problem well let's assume our K value in this case instead of being three it was actually one in that case we would want Windows of size two what would be the Brute Force way to solve this problem well just check every single window check this window are there any duplicates check this window are there any duplicates and keep doing that for every window what's the easiest way to identify duplicates usually a hash set or a hash map is pretty easy CU we can insert and look up values in constant time there's one slight Improvement here that we can make though instead of Brute Force checking every value in this window and then restarting over here and checking every value in this window and then restarting over here checking every value here we can use the sliding window technique which if you're familiar with it it's pretty obvious that it can be applied to this problem because this whole problem is about Windows so we would start our window like this we have one and two and then when we shift our window over here we keep this value in the window but we would remove this from our window and add this value to our window and check are there any duplicates here now and we would keep doing that until we got to the end of the array or that we did find duplicate values in which case all we have to do is return true we don't have to return the actual indices of those duplicates so doing it this way the time complexity will be o of n because we're using a sliding window we're never going to add the same element to the hash set more than once and the memory complexity for the hashset is going to be also o of n well actually it's more accurate to say o of K in this case where K I guess the max value it could be is n but o of K is probably more accurate here so now let's code it up so the first thing I'm going to do is initialize our window hash set this is going to keep track of all the values in our window we're going to have two pointers to Define our window we're going to have the left pointer which is going to start at the beginning and we're also going to have a right pointer but we don't need to initialize that because we can just use it in our looping here we're going to go through every position in the input array nums for every value at index R we're going to check has it already been added to our window if it has then that must mean we found a duplicate in which case we can just go ahead and return true otherwise we're going to go ahead and add that value to our window nums of R but remember the case as we shift we don't want our window to be greater than the I think it was k + one so before we even do this it's important that we make sure to do this before before we do it we have to check if our window is indeed too large which we could check by saying right minus left is greater than k then we know the window is too big because remember we know it's allowed to be less than or equal to K but if it's greater than k then that's a problem we have an invalid window in which case the easy thing to do is just to remove the leftmost value and also increment our left pointer and that's pretty much about it if we never end up returning true then out here we should probably return false and let's run the code to make sure that it works and as you can see yes it does and it's pretty efficient if this was helpful please like And subscribe if you're preparing for coding interviews check out NE code. it has a ton of free resources to help you prepare thanks for watching and hopefully I'll see you pretty soon
Original Description
🚀 https://neetcode.io/ - A better way to prepare for Coding Interviews
🥷 Discord: https://discord.gg/ddjKRXPqtk
🐦 Twitter: https://twitter.com/neetcode1
🐮 Support the channel: https://www.patreon.com/NEETcode
⭐ BLIND-75 PLAYLIST: https://www.youtube.com/watch?v=KLlXCFG5TnA&list=PLot-Xpze53ldVwtstag2TL4HQhAnC8ATf
💡 DYNAMIC PROGRAMMING PLAYLIST: https://www.youtube.com/watch?v=73r3KWiEvyk&list=PLot-Xpze53lcvx_tjrr_m2lgD2NsRHlNO&index=1
Problem Link: https://neetcode.io/problems/contains-duplicate-ii
0:00 - Read the problem
0:30 - Drawing Explanation
3:38 - Coding Explanation
leetcode 219
#neetcode #leetcode #python
Playlist
Uploads from NeetCodeIO · NeetCodeIO · 26 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
▶
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Leetcode 149 - Maximum Points on a Line - Python
NeetCodeIO
Design Linked List - Leetcode 707 - Python
NeetCodeIO
Minimum Time to Collect All Apples in a Tree - Leetcode 1443 - Python
NeetCodeIO
Design Browser History - Leetcode 1472 - Python
NeetCodeIO
Number of Good Paths - Leetcode 2421 - Python
NeetCodeIO
Flip String to Monotone Increasing - Leetcode 926 - Python
NeetCodeIO
Maximum Sum Circular Subarray - Leetcode 918 - Python
NeetCodeIO
Find Closest Node to Given Two Nodes - Leetcode 2359 - Python
NeetCodeIO
Concatenated Words - Leetcode 472 - Python
NeetCodeIO
Data Stream as Disjoint Intervals - Leetcode 352 - Python
NeetCodeIO
LFU Cache - Leetcode 460 - Python
NeetCodeIO
N-th Tribonacci Number - Leetcode 1137
NeetCodeIO
Best Team with no Conflicts - Leetcode 1626 - Python
NeetCodeIO
Greatest Common Divisor of Strings - Leetcode 1071 - Python
NeetCodeIO
Shortest Path in a Binary Matrix - Leetcode 1091 - Python
NeetCodeIO
Insert into a Binary Search Tree - Leetcode 701 - Python
NeetCodeIO
Delete Node in a BST - Leetcode 450 - Python
NeetCodeIO
Shuffle the Array (Constant Space) - Leetcode 1470 - Python
NeetCodeIO
Fruits into Basket - Leetcode 904 - Python
NeetCodeIO
Number of Subarrays of size K and Average Greater than or Equal to Threshold - Leetcode 1343 Python
NeetCodeIO
Naming a Company - Leetcode 2306 - Python
NeetCodeIO
As Far from Land as Possible - Leetcode 1162 - Python
NeetCodeIO
Shortest Path with Alternating Colors - Leetcode 1129 - Python
NeetCodeIO
Minimum Fuel Cost to Report to the Capital - Leetcode 2477 - Python
NeetCodeIO
Count Odd Numbers in an Interval Range - Leetcode 1523 - Python
NeetCodeIO
Contains Duplicate II - Leetcode 219 - Python
NeetCodeIO
Path with Maximum Probability - Leetcode 1514 - Python
NeetCodeIO
Add to Array-Form of Integer - Leetcode 989 - Python
NeetCodeIO
Unique Paths II - Leetcode 63 - Python
NeetCodeIO
Minimum Distance between BST Nodes - Leetcode 783 - Python
NeetCodeIO
Design Hashmap - Leetcode 706 - Python
NeetCodeIO
Range Sum Query Immutable - Leetcode 303 - Python
NeetCodeIO
Binary Tree Zigzag Level Order Traversal - Leetcode 103 - Python
NeetCodeIO
Middle of the Linked List - Leetcode 876 - Python
NeetCodeIO
Course Schedule IV - Leetcode 1462 - Python
NeetCodeIO
Single Element in a Sorted Array - Leetcode 540 - Python
NeetCodeIO
Capacity to Ship Packages - Leetcode 1011 - Python
NeetCodeIO
IPO - Leetcode 502 - Python
NeetCodeIO
Minimize Deviation in Array - Leetcode 1675 - Python
NeetCodeIO
Longest Turbulent Array - Leetcode 978 - Python
NeetCodeIO
Last Stone Weight II - Leetcode 1049 - Python
NeetCodeIO
Construct Quad Tree - Leetcode 427 - Python
NeetCodeIO
Find Duplicate Subtrees - Leetcode 652 - Python
NeetCodeIO
Sort an Array - Leetcode 912 - Python
NeetCodeIO
Ones and Zeroes - Leetcode 474 - Python
NeetCodeIO
Remove Duplicates from Sorted Array II - Leetcode 80 - Python
NeetCodeIO
Maximum Twin Sum of a Linked List - Leetcode 2130 - Python
NeetCodeIO
Concatenation of Array - Leetcode 1929 - Python
NeetCodeIO
Symmetric Tree - Leetcode 101 - Python
NeetCodeIO
Check Completeness of a Binary Tree - Leetcode 958 - Python
NeetCodeIO
Construct Binary Tree from Inorder and Postorder Traversal - Leetcode 106 - Python
NeetCodeIO
Find Peak Element - Leetcode 162 - Python
NeetCodeIO
Accounts Merge - Leetcode 721 - Python
NeetCodeIO
Binary Tree Preorder Traversal (Iterative) - Leetcode 144 - Python
NeetCodeIO
Binary Tree Postorder Traversal (Iterative) - Leetcode 145 - Python
NeetCodeIO
Number of Zero-Filled Subarrays - Leetcode 2348 - Python
NeetCodeIO
Minimum Score of a Path Between Two Cities - Leetcode 2492 - Python
NeetCodeIO
Sqrt(x) - Leetcode 69 - Python
NeetCodeIO
Successful Pairs of Spells and Potions - Leetcode 2300 - Python
NeetCodeIO
Optimal Partition of String - Leetcode 2405 - Python
NeetCodeIO
More on: Algorithm Basics
View skill →Related Reads
📰
📰
📰
📰
Understanding Algorithm Running Time
Medium · Programming
Common Methods to Find GCD
Medium · Programming
Recursion vs Iteration: Choosing Your Path Like Neo in *The Matrix*
Dev.to · Timevolt
You Don’t Need to Solve 500 LeetCode Problems. You Need to Recognize 12 Patterns.
Medium · Programming
Chapters (3)
Read the problem
0:30
Drawing Explanation
3:38
Coding Explanation
🎓
Tutor Explanation
DeepCamp AI