💻 Day 89 of #100DaysOfCoding Challenge 📘 Problem: LeetCode 219 — Contains Duplicate II 🎯 Level: Easy 🧩 Problem Statement: Given an integer array nums and an integer k, return true if there are two distinct indices i and j such that nums[i] == nums[j] and abs(i - j) <= k. 🧠 Approach: Initially, I solved this problem using nested loops, but it resulted in a Time Limit Exceeded (TLE) due to O(n²) complexity. To optimize it, I used a HashMap to store each element and its latest index. This allows checking duplicates within distance k in O(1) time for each iteration — giving an overall O(n) solution. 🕒 Time Complexity: O(n) 💾 Space Complexity: O(n) ✅ Key Takeaway: Sometimes, a brute-force solution may pass small test cases but fail large ones due to inefficiency. Optimizing with appropriate data structures like HashMap or HashSet can significantly improve performance. #LeetCode #Java #HashMap #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
Optimized LeetCode 219 solution with HashMap
More Relevant Posts
-
✨ Day 61 of 100: Same Tree ✨ Today’s challenge was LeetCode 100 – Same Tree 🌳 The task: Given two binary trees, determine if they are the same — meaning both structure and node values are identical. 💡 Key Takeaways: 🔹 Strengthened understanding of recursive tree traversal (checking left and right subtrees). 🔹 Practiced comparing tree nodes carefully — both value and structure. 🔹 Reinforced concepts of DFS (Depth-First Search) and base case handling in recursion. 🧠 Approach: 1️⃣ If both nodes are null, return true. 2️⃣ If only one is null, return false. 3️⃣ Check if values are equal, and then recursively verify left and right subtrees. 🌱 This problem was a great refresher on recursion fundamentals and tree comparison logic! #LeetCode #100DaysOfCode #Day61 #Java #DSA #CodingChallenge #BinaryTree #Recursion #DepthFirstSearch
To view or add a comment, sign in
-
-
💻 Day 65 of #LeetCode100DaysChallenge Solved LeetCode 219: Contains Duplicate II — a smart problem involving hashing and sliding window logic. 🧩 Problem: Given an integer array nums and an integer k, return true if there exist two distinct indices i and j such that: nums[i] == nums[j], and |i - j| <= k. 💡 Approach — HashMap (Index Tracking): 1️⃣ Use a HashMap to store each number and its most recent index. 2️⃣ For every element nums[i]: If it’s already in the map and i - map.get(nums[i]) <= k, return true. Otherwise, update the index in the map. 3️⃣ If no pair found, return false. ⚙️ Complexity: Time: O(N) — single pass through the array. Space: O(N) — for storing indices in the map. ✨ Key Takeaways: ✅ Strengthened understanding of index-based distance checking. ✅ Efficiently applied hash maps for tracking element positions. ✅ Learned how to implement O(N) duplicate checks with window constraints. #LeetCode #100DaysOfCode #Java #HashMap #SlidingWindow #DSA #ProblemSolving #CodingChallenge #WomenInTech
To view or add a comment, sign in
-
-
🧠 LeetCode Day 97 — Path Sum (Problem 112) Today’s challenge was about checking whether a binary tree has a root-to-leaf path such that adding up all the values along the path equals a given sum. 🔍 Problem Description: Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that the sum of the node values equals targetSum. Otherwise, return false. 💡 Approach: Traverse the tree recursively. Subtract the current node’s value from targetSum. When a leaf node is reached, check if the remaining sum equals zero. Use Depth First Search (DFS) to explore all paths. 🚀 Key Learnings: Strengthened understanding of recursive tree traversal. Practiced handling base cases in recursion effectively. Improved clarity on root-to-leaf path logic in binary trees. 🌳 #Day97 of #100DaysOfCode Each challenge adds a new layer to my problem-solving skills. Binary tree problems like this sharpen the recursive mindset — thinking from root to leaf and back up! 💪 #LeetCode #CodingChallenge #Java #100DaysOfCode #BinaryTree #ProblemSolving #Recursion
To view or add a comment, sign in
-
-
💻 Day 53 of #LeetCode100DaysChallenge Solved LeetCode 160: Intersection of Two Linked Lists — a problem that tests linked list traversal, pointer manipulation, and logical thinking. 🧩 Problem: Given the heads of two singly linked lists headA and headB, return the node where the two lists intersect. If they do not intersect, return null. 💡 Approach — Two Pointer Technique: 1️⃣ Initialize two pointers a and b at headA and headB. 2️⃣ Traverse both lists. When a pointer reaches the end of one list, redirect it to the head of the other list. 3️⃣ If the lists intersect, the pointers will meet at the intersection node. 4️⃣ If not, both pointers will eventually become null. ⚙️ Complexity: Time: O(N + M) — each list is traversed at most twice. Space: O(1) — no extra data structure used. ✨ Key Takeaways: ✅ Strengthened understanding of pointer redirection and traversal synchronization. ✅ Learned an elegant approach to detect intersection without measuring lengths. ✅ Reinforced logical thinking in linked list-based problems. #LeetCode #100DaysOfCode #Java #LinkedList #Pointers #TwoPointerTechnique #DSA #CodingJourney #WomenInTech #IntersectionOfLinkedLists
To view or add a comment, sign in
-
-
🌟 Day 63 of 100 Days of LeetCode 🌟 Today’s challenge: LeetCode 101 – Symmetric Tree 🔁 🧩 Problem Summary: Given the root of a binary tree, determine whether it’s a mirror of itself (i.e., symmetric around its center). 🌲✨ 💡 Key Takeaways: 🔹 Strengthened understanding of recursion in binary trees. 🔹 Learned how to compare mirror nodes (left ↔ right). 🔹 Realized that symmetry problems are perfect practice for recursive thinking and structural comparison. 🧠 Approach: 1️⃣ The tree is symmetric if its left and right subtrees are mirror images. 2️⃣ Use a helper function to compare: left.val == right.val left.left == right.right and left.right == right.left 3️⃣ Handle null checks carefully to avoid exceptions. 🔍 This problem beautifully shows how mirror logic + recursion can simplify complex tree checks! #LeetCode #100DaysOfCode #Java #DSA #BinaryTree #ProblemSolving #CodingJourney #Recursion #SymmetricTree
To view or add a comment, sign in
-
-
💼 Day 54 of #100DaysOfCode Challenge 🚀 📘 Problem: LeetCode #217 – Contains Duplicate 🧠 Topic: HashSet | Array | Data Structures 🔍 Problem Statement: Given an integer array nums, return true if any value appears at least twice in the array, and false if every element is distinct. ⚙️ Approach: Used a HashSet to check for duplicates efficiently. Traverse through each number in the array. If the number already exists in the set → return true. Otherwise, add it to the set and continue. If loop ends with no duplicates → return false. 💻 Java Solution: import java.util.HashSet; public class Solution { public boolean containsDuplicate(int[] nums) { HashSet<Integer> set = new HashSet<>(); for (int num : nums) { if (set.contains(num)) { return true; } set.add(num); } return false; } } ⏱ Complexity Analysis: Time: O(n) Space: O(n) 💬 Takeaway: Simple but powerful — using the right data structure like HashSet can make problems easy and efficient to solve. ⚡ #100DaysOfCode #Day54 #LeetCode #Java #CodingChallenge #DSA #HashSet #ProblemSolving #Programmer #TechJourney
To view or add a comment, sign in
-
-
🚀 Just Solved LeetCode #1367 — Linked List in Binary Tree 📘 Problem: Given the head of a linked list and the root of a binary tree, determine whether all the elements of the linked list appear as a downward path in the binary tree. Downward means starting from any node and moving only to child nodes. Example: Input → head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] Output → true Explanation → Nodes in blue form a subpath in the binary tree. 🧠 My Approach: I used recursion to check whether the linked list sequence can be found starting from any node in the binary tree. 1️⃣ For each node in the tree, check if the list starting from `head` matches the downward path from that node. 2️⃣ If not, recursively check the left and right subtrees. 3️⃣ Used a helper function `checkPath()` to verify if a valid path continues as the recursion goes deeper. 💡 What I Learned: ✅ How to combine linked list and binary tree traversal logic ✅ How recursion can efficiently check multiple starting points ✅ Improved my understanding of tree path traversal and backtracking logic #LeetCode #Java #DSA #BinaryTree #LinkedList #CodingUpdate #LearningByDoing
To view or add a comment, sign in
-
-
🔢 Day 47 of #LeetCode100DaysChallenge Solved LeetCode 79: Word Search — a classic backtracking problem that beautifully blends recursion and grid traversal. 🧩 Problem: Given a 2D grid of characters and a word, determine if the word can be formed using sequentially adjacent cells (up, down, left, right), where each cell can be used only once. 💡 Approach Used — Backtracking (DFS): 1️⃣ Start from each cell that matches the first character. 2️⃣ Explore in all four directions recursively. 3️⃣ Temporarily mark visited cells to avoid reuse. 4️⃣ If the entire word is matched, return true; otherwise, backtrack. ⚙️ Complexity: Time: O(N × 4ᴸ) — where N is the total number of cells, and L is the word length. Space: O(L) — recursion depth. ✨ Key Takeaways: ✅ Strengthened understanding of recursion and backtracking. ✅ Learned to manage visited states effectively in grid problems. ✅ Great exercise in applying DFS to real-world matrix traversal cases. #LeetCode #100DaysOfCode #Java #Backtracking #Recursion #ProblemSolving #DSA #WomenInTech #CodingJourney
To view or add a comment, sign in
-
-
🚀 LeetCode Progress Update: Invert Binary Tree (Problem 226) 🌳 About the Problem: The task was to invert a binary tree — flipping it by swapping every left and right child node. It’s a classic problem that tests recursion and tree traversal logic. 🧠 My Approach: I went with a recursive approach. At each node, I swapped the left and right subtrees, then called the same logic for both sides. Once the node became null, the recursion stopped automatically. 💡 What I Learned: ✅ How recursion travels through a tree structure ✅ Why defining a clear base condition matters ✅ How simple logic can transform an entire data structure 🎯 Takeaway: This problem reinforced that not every challenge needs complexity — sometimes, the cleanest recursive idea is the smartest one. #LeetCode #Java #CodingJourney #ProblemSolving #DSA
To view or add a comment, sign in
-
-
Day 51 of My DSA Challenge Problem: Count all triplets (i, j, k) in an array such that arr[i] + arr[j] + arr[k] = target Example: Input → arr = [1, 2, 3, 4, 5], target = 9 Output → 2 (Triplets: [1,3,5] and [2,3,4]) My Approach: Instead of using a brute force O(N³) approach, I optimized it using HashMap + Two Loops. Logic Breakdown: Store frequency of all elements (from index 2 onwards) in a HashMap. Fix j, iterate i < j, and calculate target - (arr[i] + arr[j]). If that value exists in the HashMap → it forms valid triplets! After processing each j, reduce the count of future elements to maintain correctness. Time Complexity: O(N²) Space Complexity: O(N) #Day51 #DSAChallenge #HashMap #ProblemSolving #TripletsSum #DSA #CodingChallenge #Java #100DaysOfCode #CodeEveryday #GeeksforGeeks #LeetCode #DataStructures #Algorithms #TwoPointers #DSAwithJava #CodingCommunity #ProgrammingJourney #DeveloperMindset #TechLearning
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development