🚀 Day 25 of my DSA Journey (LeetCode + Java) Today’s practice focused on frequency-based problems, using HashMaps, Priority Queues, and simple array traversal. Each question had a different flavor but all were fun to solve! ✅ Problems Solved Today: LC 347 – Top K Frequent Elements LC 451 – Sort Characters by Frequency LC 414 – Third Maximum Number 🧠 My Experience Solving These: 🔸 347. Top K Frequent Elements A classic frequency + heap problem. First, I created a HashMap to count occurrences Then pushed elements into a max-heap/min-heap based on frequency Extracted the top k frequent elements This problem is a great example of how HashMap + PriorityQueue together give an optimal O(n log k) solution. 🔸 451. Sort Characters by Frequency This was a fun problem because it combines strings with frequency sorting. My steps: Count frequency using HashMap Use a max-heap to sort characters by highest frequency Build the final string by repeating each character its number of times Very satisfying to see the string rearranged cleanly based on frequencies. 🔸 414. Third Maximum Number This one was the simplest among the three. I used a single pass approach: Track the largest, second largest, and third largest values Update them as we scan through the array Return the third max or highest max if not enough distinct numbers exist No sorting needed — an O(n) solution with clean logic. 📌 Key Takeaway Today: Frequency-based problems are powerful and appear everywhere. ✔ HashMap is the backbone ✔ PriorityQueue/Heap helps extract top elements ✔ Many problems reduce to counting + ordering ✔ Always aim for O(n log k) or O(n) instead of sorting everything Feeling more confident every day! On to Day 26! 🚀 #DSA #LeetCode #Java #SlidingWindow #ProblemSolving #CodingJourney #Consistency #LearningDaily
Day 25 DSA Journey: Frequency Problems with HashMaps and Heaps
More Relevant Posts
-
🚀 Day 29 of my DSA Journey (LeetCode + Java) Today’s focus was on Prefix Sum + Hashing — a powerful combination for solving subarray problems efficiently. Once I understood how prefix sums and hash maps work together, these problems became much clearer and more optimized. ✅ Problems Solved Today: LC 523 – Continuous Subarray Sum LC 325 – Maximum Size Subarray Sum Equals k LC 974 – Subarray Sums Divisible by K 🧠 My Experience Solving These: 🔸 LC 523 – Continuous Subarray Sum This problem was tricky at first. I learned that if two prefix sums have the same remainder when divided by k, the subarray between them is divisible by k. Using a HashMap to store remainder and index helped me solve this in O(n) time. 🔸 LC 325 – Maximum Size Subarray Sum Equals k Here, the goal was to find the longest subarray with sum k. I used prefix sum + HashMap and stored the first occurrence of each sum to maximize subarray length. Simple idea, but very powerful. 🔸 LC 974 – Subarray Sums Divisible by K This problem focused on counting subarrays. Instead of storing indices, I stored frequency of remainders in a HashMap. Each repeated remainder formed valid subarrays divisible by k. 📌 Key Takeaway Today: Prefix Sum + Hashing can solve many subarray problems efficiently. ✔ Convert subarray logic into prefix sums ✔ Use HashMap to track sums or remainders ✔ Handle negative values carefully ✔ One pass → O(n) optimized solutions Every day I’m learning how to recognize patterns faster and write cleaner, optimized code. 💪 On to Day 30 🚀 #DSA #LeetCode #Java #PrefixSum #HashMap #ProblemSolving #CodingJourney #Consistency #LearningDaily
To view or add a comment, sign in
-
🚀 Day 28 of my DSA Journey (LeetCode + Java) Today’s focus was completely on Sliding Window + Hashing patterns. All three problems looked different, but the core logic was about maintaining a valid window and updating the answer efficiently. ✅ Problems Solved Today: LC 3 – Longest Substring Without Repeating Characters LC 424 – Longest Repeating Character Replacement LC 1004 – Max Consecutive Ones III 🧠 My Experience Solving These: 🔸 3. Longest Substring Without Repeating Characters This is a classic sliding window + HashMap problem. Used a HashMap to store last index of characters If a character repeats, moved left pointer smartly Calculated window length using right - left + 1 Very clean logic once the window movement is clear. 🔸 424. Longest Repeating Character Replacement This one was tricky but powerful. Maintained frequency array for characters Tracked the most frequent character (maxCount) If window size – maxCount > k → shrink window Understanding why maxCount doesn’t decrease was the key insight. 🔸 1004. Max Consecutive Ones III Sliding window on array with condition. Count number of zeros in the window If zeros > k → move left pointer Always track maximum window length Simple idea, very effective solution. 📌 Key Takeaway Today: Sliding Window problems are all about maintaining a valid window. ✔ Decide what makes window invalid ✔ Expand using right pointer ✔ Shrink using left pointer ✔ Update answer at every step ✔ Avoid nested loops → O(n) always wins The more I practice, the faster I recognize patterns. 💪 Consistency + daily effort = confidence. On to Day 29 🚀 #DSA #LeetCode #Java #SlidingWindow #HashMap #ProblemSolving #CodingJourney #Consistency #LearningDaily
To view or add a comment, sign in
-
Day25 - LeetCode Journey Solved LeetCode 344: Reverse String in Java ✅ This was a simple problem on the surface, but it perfectly highlights how powerful clean logic can be. The goal was to reverse a string in-place using O(1) extra space, which means no extra arrays and no shortcuts. Just pure two-pointer logic. Using the left and right pointers and swapping characters step by step felt very satisfying. It’s one of those patterns that looks small but appears everywhere in interviews and real-world problems. Mastering this makes many string and array problems much easier later on. What I liked about this problem is how it teaches efficiency. Instead of creating new memory, we directly modify the existing array. That mindset of optimizing space is extremely important in DSA. Key takeaways: • Strong practice of the two-pointer technique • In-place operations with constant extra space • Clean and readable swapping logic • Reinforced fundamentals of string manipulation ✅ Accepted successfully ✅ 0 ms runtime with optimal performance Sometimes the simplest problems build the strongest foundations. Consistency with basics is what makes complex problems feel easier later 💪 #LeetCode #DSA #Java #Strings #ProblemSolving #Algorithms #CodingJourney #InterviewPreparation #TwoPointers #Consistency #DailyPractice
To view or add a comment, sign in
-
-
📝 Day 19/30 – LeetCode #33 (Search in Rotated Sorted Array) | Java This problem combines binary search with array rotation, making it more about logic than syntax. The main challenge was identifying which half of the array is sorted at every step and deciding whether the target lies within that range. By comparing boundary values and narrowing the search space accordingly, the problem can be solved efficiently in O(log n) time. This reinforced how adaptable binary search becomes when combined with careful condition checks. Another reminder that mastering patterns is far more important than memorizing solutions. #LeetCode #Java #DSA #BinarySearch #Arrays #ProblemSolving #LearningInPublic
To view or add a comment, sign in
-
-
Day22 - LeetCode Journey Solved LeetCode 242: Valid Anagram in Java ✅ This problem was a nice reminder of how simple ideas can lead to clean and effective solutions. Checking whether two strings are anagrams really comes down to understanding character frequency and order. By sorting both strings and comparing them character by character, the logic becomes very straightforward and easy to follow. What I liked about this problem is how it strengthens fundamentals of string handling and arrays. It also shows how preprocessing data can simplify the main comparison logic. Small steps like converting strings to character arrays and sorting them make the solution both readable and reliable. Key takeaways: • Better understanding of string to array conversion • Using sorting as a tool to simplify comparisons • Writing clean and minimal logic • Strengthening basics of string manipulation ✅ All test cases passed successfully ✅ Logic kept simple and efficient ✅ Another step forward in building strong DSA foundations Consistency with such problems really builds confidence over time 💪 #LeetCode #Java #DSA #Strings #ValidAnagram #ProblemSolving #CodingJourney #InterviewPreparation #Algorithms #Consistency #LearningEveryday
To view or add a comment, sign in
-
-
Problem of the Day: Minimum Absolute Difference (LeetCode) Today I tackled the classic Minimum Absolute Difference problem. The goal is simple yet elegant: . Given an array of integers, find all pairs with the smallest absolute difference. Here’s my clean and efficient Java solution: ...................................................................................................................................................... class Solution { public List<List<Integer>> minimumAbsDifference(int[] arr) { Arrays.sort(arr); int min=Integer.MAX_VALUE; for(int i=1;i<arr.length;i++){ min=Math.min(min,(arr[i]-arr[i-1])); } List<List<Integer>> l=new ArrayList<>(); for(int i=1;i<arr.length;i++){ if((arr[i]-arr[i-1])==min){ l.add(Arrays.asList(arr[i - 1], arr[i])); } } return l; } } ........................................................................................................................................................ Key Takeaways: Sorting simplifies the problem by ensuring differences are checked only between consecutive elements. A two-pass approach (first to find the minimum difference, second to collect pairs) keeps the logic clear and modular. Time complexity: O(n log n) due to sorting, which is optimal here. ✨ Solving problems like these sharpens algorithmic thinking and prepares us for real-world scenarios where efficiency matters. #Java #LeetCode #ProblemSolving #CodingChallenge #DataStructuresAndAlgorithms
To view or add a comment, sign in
-
🚀 Day 14 – Array vs ArrayList in Java (Key Differences & Why Arrays Still Matter) Why learn Array when we already have ArrayList? 🤔 At first I thought: “Why bother learning arrays when ArrayList exists?” But the truth is… 👉 You can’t master ArrayList without mastering Array first. Understanding data structures is a must for writing efficient Java programs. Today I compared Array and ArrayList and learned why arrays are still very important. 🧠 Why Array is Important? ✅ Foundation of all data structures ✅ Used internally by ArrayList, HashMap, etc. ✅ Faster access (O(1)) ✅ Less memory usage ✅ Works with primitive types (int, double, etc.) ✅ Best choice for performance-critical code 💡 When to use what? ✔ Use Array → When size is fixed & performance matters ✔ Use ArrayList → When size changes frequently & flexibility is needed 🔥 Final Thought Master arrays first. Advanced collections become easy automatically. #Day14 #Java #Array #ArrayList #DSA #JavaDeveloper #LearningJourney #ProgrammingBasics #Consistency
To view or add a comment, sign in
-
While solving LeetCode today, I noticed an interesting Java behavior. Arrays in Java are automatically initialized to default values (0, false, null), but local variables don’t get any default value at all — using them without initialization causes a compile-time error. I dug into why this happens and learned that Java enforces this to improve type safety and prevent bugs caused by unpredictable or garbage values. This is actually pretty useful in practice: it forces us to be explicit with local variables, catches mistakes early at compile time, and helps avoid subtle runtime bugs — especially in algorithms and system-level code. It might be something many people already know, but revisiting these fundamentals while practicing DSA has been surprisingly valuable(atleast to me). #Java #LeetCode #DataStructures #SoftwareEngineering #LearningInPublic
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