📘 DSA Journey — Day 18 Today’s focus: Sliding Window with uniqueness constraint. Problem solved: • Maximum Erasure Value (LeetCode 1695) Concepts used: • Sliding Window / Two-pointer technique • HashMap for tracking elements • Maintaining running sum Key takeaway: The goal is to find the maximum sum of a subarray with all unique elements. Using a sliding window, we expand the window while elements are unique. If a duplicate is encountered, we shrink the window from the left until the duplicate is removed. A HashMap helps track the presence (or last occurrence) of elements efficiently. At the same time, we maintain a running sum of the current window. Whenever the window is valid (all unique), we update the maximum sum. This approach avoids recalculating subarrays and reduces the complexity to O(n). This problem highlights how combining sliding window + HashMap + running sum can efficiently handle uniqueness constraints. Continuing to strengthen pattern recognition and consistency in solving DSA problems. #DSA #Java #LeetCode #CodingJourney
Max Erasure Value with Sliding Window and HashMap
More Relevant Posts
-
📘 DSA Journey — Day 19 Today’s focus: Sliding Window with uniqueness and distance constraints. Problems solved: • Minimum Consecutive Cards to Pick Up (LeetCode 2260) • Substrings of Size Three with Distinct Characters (LeetCode 1876) Concepts used: • Sliding Window / Two-pointer technique • HashMap / Set for tracking duplicates • Window size and distance calculation Key takeaway: In Minimum Consecutive Cards to Pick Up, the goal is to find the smallest subarray containing duplicate elements. A HashMap is used to store the last index of each card. When a duplicate is found, we calculate the distance between indices and update the minimum length. In Substrings of Size Three with Distinct Characters, a fixed-size sliding window (size = 3) is used. We check whether all characters in the window are distinct using a set or frequency array, and count such valid substrings. Both problems highlight how tracking element positions or uniqueness helps efficiently solve problems involving constraints on subarrays or substrings. Continuing to strengthen sliding window patterns and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 20 Today’s focus: Sliding Window with frequency constraints. Problem solved: • Maximum Number of Occurrences of a Substring (LeetCode 1297) Concepts used: • Sliding Window technique • Frequency tracking using HashMap / array • Substring counting Key takeaway: The goal is to find the maximum frequency of any substring that satisfies: • At most maxLetters distinct characters • Length between minSize and maxSize A key observation simplifies the problem: We only need to check substrings of size minSize, because larger substrings will have equal or lower frequency. Using a sliding window of fixed size minSize, we: • Track character frequencies inside the window • Ensure the number of distinct characters ≤ maxLetters • Count valid substrings using a HashMap This avoids checking all possible substring sizes and reduces complexity significantly. This problem highlights how constraints can reduce the search space, making sliding window solutions more efficient. Continuing to strengthen pattern recognition and consistency in solving DSA problems. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 28 Today’s focus: Binary Search for minimum in rotated arrays. Problem solved: • Find Minimum in Rotated Sorted Array (LeetCode 153) Concepts used: • Binary Search • Identifying unsorted half • Search space reduction Key takeaway: The goal is to find the minimum element in a rotated sorted array. Using binary search, we compare the mid element with the rightmost element: • If nums[mid] > nums[right] → minimum lies in the right half • Else → minimum lies in the left half (including mid) This works because the rotation creates one unsorted region, and the minimum always lies in that region. By narrowing the search space each time, we achieve O(log n) time complexity. This problem highlights how slight modifications in array structure still allow binary search to work efficiently with the right observations. Continuing to strengthen binary search patterns and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
“Most people try to overcomplicate this one… but the simplest approach wins.” Day 69 — LeetCode Progress Problem: Height Checker Required: Given an array of student heights, return the number of indices where the heights are not in the expected non-decreasing order. Idea: If we sort the array, we get the expected order. Now just compare the original array with the sorted version — mismatches are the answer. Approach: Create a copy of the original array Sort the copied array Traverse both arrays: Compare elements at each index If they differ → increment count Return the count Time Complexity: O(n log n) Space Complexity: O(n) #LeetCode #DSA #Java #Arrays #Sorting #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 𝗗𝗮𝘆 - 36/60 Problem: Indexes of Subarray Sum 🔍 Learned: Since the array contains only non-negative numbers, used the sliding window (two-pointer) approach. Expand the window by increasing the right pointer and shrink it when the sum exceeds the target. 😅 Struggles: Initially thought about prefix sum + hashmap, but that’s unnecessary here. Realizing that non-negative elements allow sliding window made the solution much simpler. 🧠 Key Learning: Sliding window works perfectly when elements are non-negative because the sum behaves predictably. This helps achieve O(n) time complexity efficiently. 📦 Concepts Used: #Arrays #SlidingWindow #TwoPointers #Subarrays #TimeComplexity #DSA Right approach depends on constraints—understanding that is the real skill. 🚀 #Java #CodingJourney #ProblemSolving #DSA
To view or add a comment, sign in
-
-
Day 42 of Daily DSA 🚀 Solved LeetCode 921: Minimum Add to Make Parentheses Valid ✅ Problem: Given a parentheses string s, return the minimum number of moves required to make it valid. Rules: A valid string has balanced opening and closing brackets You can insert a parenthesis at any position Return the minimum additions needed Approach: Used a Stack + Counter approach to track unmatched parentheses. Steps: Traverse the string Push '(' onto stack If ')' and stack has '(' → pop Else → increment counter (unmatched closing) At the end → remaining stack size = unmatched openings Result = unmatched openings + unmatched closings ⏱ Complexity: • Time: O(n) • Space: O(n) 📊 LeetCode Stats: • Runtime: 1 ms (Beats 67.19%) ⚡ • Memory: 43.08 MB Nice extension of the Valid Parentheses problem, focusing on fixing invalid cases instead of just checking them. #DSA #LeetCode #Java #Stack #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
=>Day 17/90 DSA Journey Solved: Continuous Subarray Sum (LeetCode) Today I worked on an interesting problem involving Prefix Sum + HashMap, which helped me optimize from a brute-force O(n²) solution to an efficient O(n) approach. If the same remainder (sum % k) appears again, it means the subarray between those indices has a sum divisible by k. -> What I learned: How to use prefix sum effectively Importance of storing remainders in HashMap Handling edge cases like subarray length ≥ 2 ->Optimization: Brute Force → O(n²) ❌ Optimized Approach → O(n) ✅ #LeetCode #DSA #Java #Coding #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
Day 62 of my 365-Day DSA Challenge 🔥 Solved LC 7 — Reverse Integer (Medium) | Runtime: 1ms ⚡ But here's the real story --> I got accepted on my first attempt, and then I spotted my own bug. My original solution had no overflow guard. The line: rev = rev * 10 + original % 10 ...could silently overflow an int, and I wouldn't even know it. LeetCode just happened not to hit that edge case hard enough. The fix? Check BEFORE multiplying: → If rev > MAX_VALUE / 10, it will overflow → return 0 → If rev == MAX_VALUE / 10 and next digit > 7 → return 0 (Same logic for the negative side with MIN_VALUE and -8) This is why understanding WHY your code works matters more than just seeing "Accepted." A passing test ≠ correct code. 365 days. One problem at a time. 💪 #DSA #LeetCode #Java #365DayChallenge #SoftwareEngineering #CodingJourney
To view or add a comment, sign in
-
-
Day 46 of my POTD Challenge Today’s problem was Gray Code. The task is to generate a sequence of n-bit numbers such that two consecutive numbers differ by only one bit. Problem Idea: In normal binary counting, multiple bits can change at once. But in Gray Code, we ensure that only one bit changes between adjacent numbers. Core Insight: There is a simple pattern to generate Gray Code: 👉 Start with n = 1: 0, 1 👉 For every next step: Take the previous sequence Add 0 in front of all numbers Then take the reverse of the sequence and add 1 in front Example (n = 2): 0, 1 → add 0 → 00, 01 → reverse + add 1 → 11, 10 Final → 00, 01, 11, 10 Approach I used: Start with base case Use reflection method to build sequence step by step Convert binary to integer if required What I learned: Learned a new pattern-based generation technique Understood how reflection helps maintain constraints Improved thinking about bit manipulation problems A very interesting problem that combines patterns and bit logic. #POTD #DSA #Java #BitManipulation #GrayCode #CodingChallenge #ProblemSolving #LearningInPublic #100DaysOfCode #TechJourney
To view or add a comment, sign in
-
-
🚀 Day 27/100 – LeetCode DSA Challenge 📌 Problem Solved: Find All Numbers Disappeared in an Array Today’s problem focused on identifying missing numbers from an array where elements are in the range [1, n]. 🔍 Approach I Used (Easy Method): Created a boolean array to track visited numbers Marked each number as present Iterated from 1 to n to find missing elements 💡 Key Concepts Learned: How to use index-based mapping for arrays Importance of range constraints [1, n] Difference between brute force vs optimized approach Using extra space to simplify logic 🧠 What I Understood Today: If a problem says numbers are in range [1, n], we can directly map values to indices Tracking frequency or presence is a powerful technique Writing clean and simple logic first is more important than jumping to optimization 🔥 Next Step: I will try the optimized approach (O(1) space) using index marking (negative marking technique) 📈 Improving step by step in DSA — consistency is the key! #Day27 #LeetCode #DSA #100DaysOfCode #CodingJourney #PlacementsPreparation #Java
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