📌 Day 17/100 - Design HashSet (LeetCode 705) 🔹 Problem: Design a HashSet without using built-in hashing libraries. Support add(key), remove(key), and contains(key) efficiently. 🔹 Approach: Used a boolean array where the index directly maps to the key. This gives O(1) access to mark presence or absence: add(key) → arr[key] = true remove(key) → arr[key] = false contains(key) → return arr[key] 🔹 Time Complexity: O(1) for add, remove, and contains 🔹 Space Complexity: O(N), where N is the range of possible keys (e.g., 1,000,001) 🔹 Key Learnings: Simple data representations can be the most performant for bounded key ranges. Always consider time vs memory trade-offs when designing data structures. Recreating core data structures reinforces how built-ins work under the hood. #100DaysOfCode #LeetCode #Java #DSA #ProblemSolving #HashSet #CodingJourney
Designing a HashSet without built-in libraries for LeetCode 705
More Relevant Posts
-
📌 Day 15/100 - Majority Element (LeetCode 169) 🔹 Problem: Given an array of integers nums, find the element that appears more than ⌊ n/2 ⌋ times — the majority element. It’s guaranteed that such an element always exists in the array. 🔹 Approach: Implemented the Boyer–Moore Voting Algorithm, a clever and efficient approach: Assume the first element as the candidate. Traverse the array — increment votes if the element matches the candidate, otherwise decrement. If votes reach zero, update the candidate. The last candidate standing is the majority element. 🔹 Time Complexity: O(n) — only one traversal through the array. 🔹 Space Complexity: O(1) — uses constant extra space. 🔹 Key Learnings: Learned how voting logic simplifies counting-heavy problems. Achieved 99.76% faster runtime on LeetCode. Reinforced how simplicity and logic often outperform brute force. 💡 Every dataset has a voice — you just need the right logic to hear it. #100DaysOfCode #LeetCode #Java #ProblemSolving #DSA #CodingChallenge #BoyerMooreAlgorithm
To view or add a comment, sign in
-
-
🚀 Day 87/100 of #100DaysOfCode 💡 Problem: 3321. Find X-Sum of All K-Long Subarrays II (Hard) 🔗 Platform: LeetCode Today’s challenge was all about optimizing sliding window logic with frequency-based ordering! The task — find the “x-sum” of every k-sized subarray, keeping only the top x most frequent elements (and if tied, prefer larger values). 🧠 Key Learnings: How to maintain frequency counts efficiently in a sliding window. Managing top-x elements dynamically using TreeSet and custom comparators. Overcoming TLE issues by switching from naive sorting (O(n*k)) to a balanced TreeSet approach (O(n log n)). ⚙️ Concepts Used: → Sliding Window → Frequency Map (HashMap) → Ordered Sets (TreeSet) → Custom Comparator Logic 🔥 This one really tested both logic & optimization — but once it clicked, it felt amazing! #LeetCode #100DaysOfCode #Java #CodingChallenge #ProblemSolving #DSA #SlidingWindow #Optimization
To view or add a comment, sign in
-
-
✅ Day 75 of LeetCode Medium/Hard Edition Today’s challenge was “Range Add Queries 2D” — a fascinating problem that tests your understanding of grid manipulation, optimization, and prefix-sum based range updates ⚡💡 📦 Problem: You're given an n × n matrix initialized with zeros. Each query specifies a rectangle (r1, c1) to (r2, c2), and you must add +1 to every cell inside this rectangle. Return the final matrix after applying all queries. 🔗 Problem Link: https://lnkd.in/g9ct4VtM ✅ My Submission: https://lnkd.in/gkSe-sP8 💡 Thought Process: A direct brute-force update for each query results in O(q × n²) complexity — too slow for large constraints. The key insight is to replace repeated cell updates with a 2D Difference Array, which allows you to update an entire rectangle using only four operations: Add +1 at the start point Subtract at the boundaries Propagate values through prefix sums After building the difference matrix, we use: Row-wise prefix sums Column-wise prefix sums This reconstructs the final grid efficiently. This method transforms the problem from potentially billions of operations into a neat and fast O(q + n²) solution. 🎯 How the Difference Array Helps: Avoids updating each cell individually Captures changes in a compact form Spreads values using cumulative sums Works perfectly for rectangular range additions ⚙️ Complexity: ⏱ Time: O(q + n²) with prefix sums 💾 Space: O(n²) for the difference matrix #LeetCodeMediumHardEdition #100DaysChallenge #PrefixSum #Optimization #Java #DSA #ProblemSolving #LearnEveryday
To view or add a comment, sign in
-
-
🌳 Day 60 of #100DaysOfCode 🌳 🔹 Problem: Balanced Binary Tree – LeetCode ✨ Approach: Used a post-order DFS traversal to calculate subtree heights while checking balance at every node. If the height difference of any subtree exceeds 1, return -1 immediately for an early exit — efficient and elegant! ⚡ 📊 Complexity Analysis: Time: O(n) — each node visited once Space: O(h) — recursion stack space, where h is the tree height ✅ Runtime: 0 ms (Beats 100%) ✅ Memory: 44.29 MB 🔑 Key Insight: A balanced tree isn’t just about equal heights — it’s about smart recursion that detects imbalance early, saving both time and memory. 🌿 #LeetCode #100DaysOfCode #Java #DSA #BinaryTree #Recursion #ProblemSolving #AlgorithmDesign #CodeJourney #ProgrammingChallenge
To view or add a comment, sign in
-
-
🚀 Day 46 of #100DaysOfCode – Striver’s DSA Sheet 🚀 ✅ Topic Covered Today: Implement Queue Using Linked List 💡 Lesson of the Day (Approach-Focused): 🔹 Understanding the Core Idea A queue follows FIFO (First In, First Out) order. Using a Linked List, we maintain dynamic memory and perform operations efficiently with two pointers: front → points to the first node rear → points to the last node This avoids the limitations of fixed-size arrays. 🔹 1️⃣ Enqueue (Insert at Rear) Approach: Create a new node If queue is empty → front = rear = newNode Else → attach newNode at rear.next and move rear forward 🧮 Time Complexity: O(1) 💾 Space Complexity: O(1) extra 🔹 2️⃣ Dequeue (Remove from Front) Approach: If empty → nothing to remove Return node at front Move front = front.next If queue becomes empty → rear = null 🧮 Time Complexity: O(1) 🔹 3️⃣ Peek Return front.data without removing it. 🧮 Time Complexity: O(1) 🔹 4️⃣ isEmpty Check: front == null 🧮 Time Complexity: O(1) ✨ Key Idea: A linked-list-based queue never overflows (unless memory is full) and supports constant-time insertions and deletions — perfect for dynamic data scenarios. 💭 Learning: Today’s implementation strengthened my understanding of pointers and dynamic memory. Building a queue with linked nodes is a great way to visualize how real queue structures work behind the scenes. #100DaysOfCode #DSA #StriversSheet #Java #LinkedList #Queue #ProblemSolving #CodingJourney #LogicBuilding #Consistency
To view or add a comment, sign in
-
-
#Day_24 💡 Problem Solved: Intersection of Two Arrays II Today’s challenge tested my ability to handle array manipulation and two-pointer logic efficiently. 🧩 Problem Overview: Given two integer arrays, the task is to find their intersection — meaning the common elements between both arrays, including duplicates. For example: nums1 = [4,9,5] and nums2 = [9,4,9,8,4] 👉 Output should be [4,9] (since both 4 and 9 appear in both arrays). ⚙️ My Approach: Sorting both arrays: By sorting nums1 and nums2, I was able to efficiently compare elements in a linear scan. Two-pointer technique: I used two indices i and j, starting from 0 in each array. If both elements matched → it’s part of the intersection. If one was smaller → move that pointer ahead to catch up. Storing results dynamically: Added all matching elements into an ArrayList. Finally converted it into an integer array for the result. This approach ensures O(n log n) time complexity due to sorting, but O(n) in space — a solid balance between clarity and performance. 🧠 Key Takeaways: Sorting simplifies complex comparison problems. The two-pointer pattern is extremely useful for dealing with sorted arrays. Always remember: clean logic > brute force. 🔥 Every new problem brings a fresh perspective on optimizing logic and learning better ways to handle data structures. #Java #CodingChallenge #LeetCode #100DaysOfCode #ProblemSolving #DSA #ProgrammingJourney #TechWithMudassir
To view or add a comment, sign in
-
-
✅ Just solved LeetCode #654 — Maximum Binary Tree 📘 Problem: Given an integer array without duplicates, the task is to build a maximum binary tree. The construction rules are: 1️⃣ The root is the maximum element in the array. 2️⃣ The left subtree is built recursively from elements to the left of the maximum. 3️⃣ The right subtree is built recursively from elements to the right of the maximum. Example: Input → [3,2,1,6,0,5] Output → [6,3,5,null,2,0,null,null,1] 🧠 My Approach: I solved this problem using a recursive divide-and-conquer approach. 1️⃣ Find the index of the maximum element in the given range — this becomes the root. 2️⃣ Recursively build the left subtree from the subarray before the maximum element. 3️⃣ Recursively build the right subtree from the subarray after the maximum element. 💡 What I Learned: ✅ How recursion naturally fits into tree construction problems ✅ The concept of divide and conquer applied to array-based tree building ✅ How to translate problem definitions into direct recursive structure #LeetCode #Java #DSA #BinaryTree #CodingUpdate #LearningByDoing
To view or add a comment, sign in
-
-
💻 Day 68 of #LeetCode100DaysChallenge Solved LeetCode 105: Construct Binary Tree from Preorder and Inorder Traversal — a problem that tests tree construction, recursion, and array indexing skills. 🧩 Problem: Given two integer arrays, preorder and inorder, representing the preorder and inorder traversal of a binary tree, construct and return the binary tree. 💡 Approach — Recursive Tree Construction: 1️⃣ The first element in preorder is always the root. 2️⃣ Locate this root in the inorder array; elements left of it belong to the left subtree, and elements right belong to the right subtree. 3️⃣ Recursively apply the same logic to build left and right subtrees using corresponding slices of preorder and inorder arrays. 4️⃣ Return the root node after linking its left and right subtrees. ⚙️ Complexity: Time: O(N) — each node is processed once Space: O(N) — for recursion stack and storing inorder index mapping ✨ Key Takeaways: ✅ Strengthened recursive thinking for tree construction. ✅ Practiced mapping array indices to manage subtrees efficiently. ✅ Reinforced understanding of preorder and inorder traversal properties. #LeetCode #100DaysOfCode #Java #BinaryTree #Recursion #TreeConstruction #DSA #CodingJourney #WomenInTech #ProblemSolving
To view or add a comment, sign in
-
-
#Day_23 💡 Problem Solved: Intersection of Two Arrays Today’s challenge tested my ability to handle array manipulation and two-pointer logic efficiently. 🧩 Problem Overview: Given two integer arrays, the task is to find their intersection — meaning the common elements between both arrays, including duplicates. For example: nums1 = [4,9,5] and nums2 = [9,4,9,8,4] 👉 Output should be [4,9] (since both 4 and 9 appear in both arrays). ⚙️ My Approach: Sorting both arrays: By sorting nums1 and nums2, I was able to efficiently compare elements in a linear scan. Two-pointer technique: I used two indices i and j, starting from 0 in each array. If both elements matched → it’s part of the intersection. If one was smaller → move that pointer ahead to catch up. Storing results dynamically: Added all matching elements into an ArrayList. Finally converted it into an integer array for the result. This approach ensures O(n log n) time complexity due to sorting, but O(n) in space — a solid balance between clarity and performance. 🧠 Key Takeaways: Sorting simplifies complex comparison problems. The two-pointer pattern is extremely useful for dealing with sorted arrays. Always remember: clean logic > brute force. 🔥 Every new problem brings a fresh perspective on optimizing logic and learning better ways to handle data structures. #Java #CodingChallenge #LeetCode #100DaysOfCode #ProblemSolving #DSA #ProgrammingJourney #TechWithMudassir
To view or add a comment, sign in
-
-
🌟 Day 76 of #100DaysOfCodingChallenge 📘 Problem: LeetCode 73 – Set Matrix Zeroes Today, I explored an important array manipulation problem — setting entire rows and columns to zero if any element in a 2D matrix is zero. 🧩 Problem Understanding Given an m x n matrix, if any cell has the value 0, we must make its entire row and column 0. For example: Input: [ [1, 2, 3], [4, 0, 6], [7, 8, 9] ] Output: [ [1, 0, 3], [0, 0, 0], [7, 0, 9] ] 💡 My Approach (Brute Force) To handle this, I used two extra arrays — one to track rows and another to track columns that should become zero. 🪄 Steps followed: 1️⃣ Traverse the matrix and store the indices of rows and columns containing 0. 2️⃣ Loop through the matrix again and make the elements 0 wherever their row or column index is marked. This simple approach helps clearly visualize how matrix updates propagate. 🧠 Concept Learned: How to access and manipulate specific rows and columns in a 2D array. The importance of auxiliary data structures (like boolean arrays) in preserving information during traversal. 🕒 Complexity: Time: O(m × n) Space: O(m + n) 🧵 Next Step: I’ll be optimizing this approach to an O(1) space solution using the matrix itself as a marker — a neat trick in in-place algorithms! #100DaysOfCode #Day75 #LeetCode #Java #CodingChallenge #ProblemSolving #ArrayManipulation #DataStructures #WomenInTech
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