LeetCode 219: Contains Duplicate II — Clean & Optimal Solution (C++/Python)
Автор: Ajmain Fayek Diganta
Загружено: 2025-11-24
Просмотров: 4
In this video, we solve LeetCode Problem 219: Contains Duplicate II, a very popular problem involving HashMaps, Sliding Window, and efficient duplicate detection.
This problem is excellent for beginners because it teaches you how to use hashing to track indices efficiently and how to turn a brute-force O(n²) approach into an optimal O(n) solution.
🧠 Problem Logic
We are given an array nums and an integer k.
We must determine:
Does there exist two equal elements such that the absolute difference of their indices is ≤ k?
✔️ Efficient Approach (HashMap / Unordered Map)
Keep a map that stores: value → last index where it appeared
For each element:
If it exists in the map already:
Check if i - last_index ≤ k
If yes → we found the answer (return true)
Update the index in the map
This gives us:
⏱️ Time Complexity: O(n)
📦 Space Complexity: O(n)
🔗 Problem Link
https://leetcode.com/problems/contains-dup...
👋 About Me
Hi, I'm Ajmain Fayek Diganta, a CSE student at BUET.
I upload tutorials on Competitive Programming, LeetCode, Algorithms, and C++ problem solving to help students and beginners grow.
🔗 GitHub: https://github.com/DIGANTA100
🔗 LinkedIn: https://www.linkedin.com/in/ajmain-fayek-d...
🔔 Subscribe for more coding tutorials and problem explanations!
#LeetCode #LeetCode219 #ContainsDuplicateII #HashMap #SlidingWindow #CodingInterview #Programming #CPlusPlus #Python #ProblemSolving #AjmainFayekDiganta #BUET #CSE
Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: