🚀 Learning DSA: Merge Intervals (LeetCode 56) Today I worked on the classic Merge Intervals problem, which is a very common pattern in coding interviews. Problem: Given a list of intervals [start, end], merge all overlapping intervals and return the non-overlapping intervals. Example: Input: [[1,3], [2,6], [8,10], [15,18]] Output: [[1,6], [8,10], [15,18]] Key Insight: If intervals are sorted by their start time, overlapping intervals will appear next to each other. This allows us to merge them efficiently in a single pass. Approach: 1️⃣ Sort intervals based on the start value using a comparator. 2️⃣ Keep track of the current interval (start, end). 3️⃣ If the next interval overlaps (current.start ≤ previous.end), extend the end. 4️⃣ Otherwise, store the previous interval and start a new one. Time Complexity: ⏱️ O(n log n) due to sorting Space Complexity: O(n) for the result list. This problem helped me better understand the interval merging pattern, which is widely used in many problems like meeting rooms, insert intervals, and scheduling problems. 💡 Key takeaway: Sort intervals first, then merge overlapping ones in a single pass. #DSA #Java #CodingInterview #LeetCode #SoftwareEngineering #ProblemSolving #JavaProgramming #SoftwareEngineering #ProblemSolving #100DaysOfCode #CodingJourney #DeveloperCommunity #TechLearning #ComputerScience
Merging Intervals in LeetCode 56
More Relevant Posts
-
🔥 𝗨𝗽𝘀𝗼𝗹𝘃𝗶𝗻𝗴 𝗮 𝗛𝗮𝗿𝗱 𝗖𝗼𝗻𝘁𝗲𝘀𝘁 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 𝗧𝗼𝗱𝗮𝘆 — 𝗔 𝗟𝗲𝘀𝘀𝗼𝗻 𝗶𝗻 𝗔𝗿𝗿𝗮𝘆𝘀 & 𝗥𝗼𝘁𝗮𝘁𝗶𝗼𝗻𝘀 Today I upsolved a hard problem that looked like a simple rotation task but turned into a lesson in 𝗱𝗶𝘃𝗶𝘀𝗼𝗿𝘀, 𝗽𝗲𝗿𝗺𝘂𝘁𝗮𝘁𝗶𝗼𝗻𝘀, and 𝘀𝗺𝗮𝗿𝘁 𝗼𝗯𝘀𝗲𝗿𝘃𝗮𝘁𝗶𝗼𝗻. 🧩 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 Given an array nums of length n, a number k is sortable if: a. k divides n b. Split the array into blocks of size k c. You can cyclically rotate each block d. After rotations, the whole array becomes non-decreasing Return the sum of all such k. 💡 𝗞𝗲𝘆 𝗜𝗻𝘀𝗶𝗴𝗵𝘁 Instead of asking “how to rotate to sort?”, ask: What must be true if sorting by rotations is possible? ⚙️ 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 1. Only check divisors of n (valid block sizes) 2. Create a sorted copy of the array 3. For each block, check if some rotation can match its sorted segment 4. Use the minimum element as an anchor to test rotation alignment 5. If all blocks pass → add k to the answer 📚 𝗟𝗲𝗮𝗿𝗻𝗶𝗻𝗴𝘀 1. Divisors often appear in partition problems 2. Think from the final state backward 3. Use invariants (like minimum element) as anchors A great reminder that tough problems become simpler when you change perspective. #DSA #Algorithms #ProblemSolving #CompetitiveProgramming #Java #LearningInPublic #DataStructures #Coding #CodingLife #ProgrammerLife #Developers #Tech #InterviewPrep #CodingInterview #LeetCode #CP #Upsolving #DailyCoding #ArrayProblems #MathInCoding #LogicBuilding #AnalyticalThinking
To view or add a comment, sign in
-
-
🧱 𝗜 𝗦𝗼𝗹𝘃𝗲𝗱 𝗮 “𝗩𝗶𝘀𝘂𝗮𝗹” 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 𝗨𝘀𝗶𝗻𝗴 𝗢𝗻𝗹𝘆 𝗮 𝗦𝘁𝗮𝗰𝗸 — 𝗡𝗼 𝗚𝗲𝗼𝗺𝗲𝘁𝗿𝘆 𝗡𝗲𝗲𝗱𝗲𝗱 Today’s problem looked visual and innocent: 𝗚𝗶𝘃𝗲𝗻 𝗯𝗮𝗿 𝗵𝗲𝗶𝗴𝗵𝘁𝘀 𝗼𝗳 𝗮 𝗵𝗶𝘀𝘁𝗼𝗴𝗿𝗮𝗺 (𝘄𝗶𝗱𝘁𝗵 = 𝟭), 𝗳𝗶𝗻𝗱 𝘁𝗵𝗲 𝗹𝗮𝗿𝗴𝗲𝘀𝘁 𝗿𝗲𝗰𝘁𝗮𝗻𝗴𝗹𝗲 𝗮𝗿𝗲𝗮. This is the classic problem from 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 — and it’s a beautiful lesson in how 𝗺𝗼𝗻𝗼𝘁𝗼𝗻𝗶𝗰 𝘀𝘁𝗮𝗰𝗸𝘀 𝗰𝗼𝗻𝘃𝗲𝗿𝘁 𝗴𝗲𝗼𝗺𝗲𝘁𝗿𝘆 𝗶𝗻𝘁𝗼 𝗶𝗻𝗱𝗶𝗰𝗲𝘀. 🧠 𝗧𝗵𝗲 𝗡𝗮𝗶𝘃𝗲 𝗧𝗵𝗼𝘂𝗴𝗵𝘁 (𝗮𝗻𝗱 𝘄𝗵𝘆 𝗶𝘁 𝗳𝗮𝗶𝗹𝘀) For every bar i, try expanding left and right until you hit a smaller bar. That’s O(n) per index -> O(n²). Too slow for n=10^5. So the real question becomes: For each bar, how far can it extend without hitting a smaller height? That is the entire problem. 💡 𝗞𝗲𝘆 𝗜𝗱𝗲𝗮 — 𝗡𝗲𝗮𝗿𝗲𝘀𝘁 𝗦𝗺𝗮𝗹𝗹𝗲𝗿 𝗘𝗹𝗲𝗺𝗲𝗻𝘁𝘀 For every index i: 1. Find the first smaller bar on the left 2. Find the first smaller bar on the right A monotonic increasing stack gives both in linear time. ▶️ 𝗟𝗲𝗳𝘁 𝗦𝗰𝗮𝗻 (𝗟 -> 𝗥) Pop until the top is strictly smaller. That index defines the left boundary. ◀️ 𝗥𝗶𝗴𝗵𝘁 𝗦𝗰𝗮𝗻 (𝗥 -> 𝗟) Same logic to get the right boundary. Now each bar knows the exact range where it is the minimum. ⏱️ 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆 Each index pushed/popped once → O(n) Space → O(n) ✨ 𝗟𝗲𝗮𝗿𝗻𝗶𝗻𝗴𝘀 1. “Nearest smaller element” is a powerful pattern 2. Monotonic stacks turn nested loops into linear scans 3. Think: If this bar is the minimum, how wide can the rectangle be? A visual problem, solved with clean stack discipline. #DataStructures #Algorithms #MonotonicStack #Java #ProblemSolving #CodingInterview #Stack #DSA #LeetCode #Coding #Programmer #SoftwareEngineer #InterviewPrep #CompetitiveProgramming #100DaysOfCode #Tech #Learning #ProblemSolvingSkills
To view or add a comment, sign in
-
-
Most people think Time Complexity means how much time a program takes to run ❌ But that’s NOT the correct way to understand it. Here’s the real explanation 👇 🔹 Time Complexity • It does NOT measure actual time (seconds/minutes) • It measures the number of operations an algorithm performs as input size (n) increases 👉 In simple words: Time Complexity = How many steps your code executes 🔹 Example: for(int i = 0; i < n; i++) { System.out.println(i); } 👉 This loop runs n times 👉 So number of operations ≈ n ✅ Time Complexity = O(n) 🔹 Another Example: for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { System.out.println(i + " " + j); } } 👉 Outer loop → n times 👉 Inner loop → n times 👉 Total operations ≈ n × n = n² ✅ Time Complexity = O(n²) --------------------------------------------- 🔹 Space Complexity • It does NOT mean total RAM of your system ❌ • It measures the extra memory used by the program 👉 In simple words: Space Complexity = How much extra space your code needs 🔹 Example: int sum = 0; 👉 Only one variable used ✅ Space Complexity = O(1) (constant space) 🔹 Another Example: int[] arr = new int[n]; 👉 Array size depends on n ✅ Space Complexity = O(n) 🔹 Why this matters? ✅ Helps you write efficient code ✅ Important for coding interviews ✅ Used in real-world systems handling large data 💡 Final Thought: It’s not about how fast your code runs on your laptop, it’s about how your code scales when input becomes huge. Save this post if you are preparing for DSA 🚀 #DSA #TimeComplexity #SpaceComplexity #CodingInterview #Programming #Java #LearnToCode
To view or add a comment, sign in
-
Solved the Spiral Matrix problem by traversing the matrix layer by layer. The approach uses four boundaries — top, bottom, left, and right — to systematically move in a spiral direction and collect elements. By adjusting the boundaries after each traversal (left to right, top to bottom, right to left, and bottom to top), the algorithm ensures every element is visited exactly once without repetition. Time Complexity: O(n × m) Space Complexity: O(1) (excluding output list) Understanding boundary-based traversal patterns is important for solving matrix problems efficiently and writing clean, structured, and interview-ready code. #Java #DSA #ProblemSolving #Coding #SoftwareEngineering #LeetCode #Developers
To view or add a comment, sign in
-
-
🚀 Day 56 of my DSA Journey Today, I worked on a Hard-level problem from LeetCode: 👉 Problem #4 – Median of Two Sorted Arrays (LeetCode) This problem is well-known for its complexity and is frequently asked in top product-based companies. 💡 What I focused on: Understanding the problem deeply instead of jumping directly to the optimal solution Implementing a merge + sort approach to build a clear foundation Applying the two-pointer technique to efficiently identify the median Handling both odd and even length cases carefully ⚙️ Approach Used: Merged both input arrays into a single array Sorted the combined array Used two pointers (i and j) moving towards the center Determined the median based on whether the length is odd or even 📈 Key Learning: Even though the optimal solution has a time complexity of O(log(min(m, n))), building a correct and intuitive approach first is crucial. It strengthens problem-solving skills and helps in understanding advanced techniques later. 🎯 Takeaway: Consistency and clarity in logic are more important than immediately writing the most optimized code. 🔥 Step by step, moving closer to mastering Data Structures & Algorithms. #DSA #LeetCode #ProblemSolving #Java #CodingJourney #PlacementPreparation #Consistency #Learning
To view or add a comment, sign in
-
-
🚀 Day 17/50 – LeetCode Challenge 🧩 Problem: Longest Common Subsequence (LCS) Today I worked on the Longest Common Subsequence problem, which is a classic example of Dynamic Programming and appears frequently in coding interviews. 📌 Problem Summary: Given two strings, the goal is to find the length of the longest subsequence that appears in both strings in the same order (not necessarily contiguous). Example: String 1 → "abcde" String 2 → "ace" Output → 3, because "ace" is the longest common subsequence. 🔍 Approach Used ✔ Used Dynamic Programming (DP) ✔ Created a 2D table to store intermediate results ✔ Compared characters of both strings step by step ✔ If characters matched → increase the subsequence length ✔ Otherwise → take the maximum value from previous results ⏱ Time Complexity: O(m × n) 📦 Space Complexity: O(m × n) 💡 Key Learning ✔ Understanding dynamic programming patterns ✔ Solving problems with overlapping subproblems ✔ Improving logical thinking for sequence comparison problems Problems like LCS are important because they form the foundation for many advanced algorithms used in bioinformatics, text comparison, and version control systems. Consistency in problem solving is the key to improvement 🚀 #LeetCode #DSA #DynamicProgramming #ProblemSolving #CodingJourney #FutureAIEngineer #Consistency
To view or add a comment, sign in
-
-
𝐂𝐨𝐝𝐢𝐧𝐠 is the missing puzzle piece that connects everything - whether you want to go into 𝐖𝐞𝐛 𝐃𝐞𝐯𝐞𝐥𝐨𝐩𝐦𝐞𝐧𝐭, 𝐃𝐚𝐭𝐚 𝐒𝐜𝐢𝐞𝐧𝐜𝐞, 𝐃𝐞𝐯𝐎𝐩𝐬, 𝐀𝐈, or 𝐒𝐨𝐟𝐭𝐰𝐚𝐫𝐞 𝐄𝐧𝐠𝐢𝐧𝐞𝐞𝐫𝐢𝐧𝐠. When your fundamentals are strong, learning new technologies becomes 𝐞𝐚𝐬𝐢𝐞𝐫, understanding systems becomes 𝐜𝐥𝐞𝐚𝐫𝐞𝐫, and opportunities become easier to 𝐫𝐞𝐚𝐜𝐡. If your tech journey feels stuck or confusing, it may not be because you lack effort - you’re just 𝐦𝐢𝐬𝐬𝐢𝐧𝐠 𝐭𝐡𝐞 𝐫𝐢𝐠𝐡𝐭 𝐩𝐢𝐞𝐜𝐞. At Coding Blocks, we help you find that missing piece by focusing on 𝐬𝐭𝐫𝐨𝐧𝐠 𝐟𝐮𝐧𝐝𝐚𝐦𝐞𝐧𝐭𝐚𝐥𝐬, 𝐫𝐞𝐚𝐥 𝐩𝐫𝐨𝐛𝐥𝐞𝐦-𝐬𝐨𝐥𝐯𝐢𝐧𝐠, and 𝐭𝐡𝐞 𝐬𝐤𝐢𝐥𝐥𝐬 that actually matter in the industry. 𝐒𝐭𝐚𝐫𝐭 𝐰𝐢𝐭𝐡 𝐭𝐡𝐞 𝐛𝐚𝐬𝐢𝐜𝐬. 𝐁𝐮𝐢𝐥𝐝 𝐬𝐭𝐫𝐨𝐧𝐠 𝐜𝐨𝐝𝐢𝐧𝐠 𝐬𝐤𝐢𝐥𝐥𝐬. 𝐂𝐨𝐦𝐩𝐥𝐞𝐭𝐞 𝐭𝐡𝐞 𝐩𝐮𝐳𝐳𝐥𝐞 🧩 #Coding #TechSkills #CodingSkills #DSA #Java #Python #WebDevelopment #SoftwareEngineering #TechCareer #CodingJourney #Coders #ProgrammingLanguages #Programmer
To view or add a comment, sign in
-
-
I’ve been getting back into solving algorithm problems recently, and I worked through a pretty challenging one: “Delete Columns to Make Sorted III.” At first glance, it looks like a simple string problem. But once you dig in, it becomes a lot more interesting — it’s really about finding a pattern across multiple sequences. 💡 Key idea I learned: Instead of thinking about deleting columns directly, you can reframe the problem as: ➡️ “What is the longest sequence of columns that keeps all strings in sorted order?” Once you see it that way, it turns into a dynamic programming problem — similar to finding a longest increasing subsequence, but across multiple strings. 🧠 What this reinforced for me: Many “hard” problems become easier when you reframe the question Patterns like DP and subsequences show up everywhere Explaining the solution clearly is just as important as solving it I wrote a full breakdown of my approach here: 👉 https://lnkd.in/giN69zAn I’m currently focused on sharpening my problem-solving and backend thinking again. If you’re working through similar problems or preparing for interviews, I’d be interested to hear how you approach these kinds of challenges. #Java #LeetCode #Algorithms #SoftwareEngineering #ProblemSolving
To view or add a comment, sign in
-
💡 𝗣𝗿𝗲𝗳𝗶𝘅 𝗦𝘂𝗺𝘀 + 𝗔 𝗦𝘂𝗯𝘁𝗹𝗲 𝗧𝘄𝗶𝘀𝘁 𝗼𝗻 “𝗗𝗶𝘀𝘁𝗶𝗻𝗰𝘁” — 𝗧𝗼𝗱𝗮𝘆’𝘀 𝗗𝗦𝗔 𝗜𝗻𝘀𝗶𝗴𝗵𝘁 Today’s problem looked familiar: count subarrays whose sum is divisible by 𝗸. But one word changed everything - 𝗱𝗶𝘀𝘁𝗶𝗻𝗰𝘁 (by value, not by index). 🧠 𝗧𝗵𝗲 𝗕𝗮𝘀𝗲 𝗜𝗱𝗲𝗮: 𝗣𝗿𝗲𝗳𝗶𝘅 𝗦𝘂𝗺 + 𝗠𝗼𝗱𝘂𝗹𝗼 A classic idea from Introduction to Algorithms: 1. If two prefix sums give the same remainder modulo k, 2. the subarray between them has a sum divisible by k. Using a hashmap of (sum % k) -> frequency, we can count such subarrays in O(n). Simple and elegant - but incomplete. ⚠️ 𝗧𝗵𝗲 𝗖𝗮𝘁𝗰𝗵: 𝗗𝘂𝗽𝗹𝗶𝗰𝗮𝘁𝗲 𝗩𝗮𝗹𝘂𝗲 𝗦𝗲𝗾𝘂𝗲𝗻𝗰𝗲𝘀 For an array like [1, 1, 1], subarrays such as [1] occur at multiple positions. They are different by index, but the same by value. Prefix sums will count all of them. We must count them only once. 💡 𝗧𝗵𝗲 𝗜𝗻𝘀𝗶𝗴𝗵𝘁 𝗧𝗵𝗮𝘁 𝗦𝗼𝗹𝘃𝗲𝘀 𝗜𝘁 Since the array is sorted, two subarrays that: 1. end at the same value, and 2. have the same (sum % k) must represent the same sequence. So we track such patterns using a key formed by: (sum % k) and the current value, and subtract these duplicates from the earlier total. ✨ 𝗞𝗲𝘆 𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆 This problem wasn’t about finding valid subarrays - it was about removing overcounted ones cleverly. A great reminder that: Sometimes the trick lies in interpreting “𝗱𝗶𝘀𝘁𝗶𝗻𝗰𝘁” correctly. #DSA #Algorithms #PrefixSum #Hashing #Java #ProblemSolving #CompetitiveProgramming #CodingInterview #DataStructures #CodingLife #Programmer #Developers #TechInterview #LeetCode #CodeNewbie #100DaysOfCode #LearningInPublic #CodingJourney #InterviewPreparation
To view or add a comment, sign in
-
-
𝙔𝙤𝙪 𝙙𝙤𝙣’𝙩 𝙣𝙚𝙚𝙙 𝙩𝙤 𝙨𝙤𝙡𝙫𝙚 100𝙨 𝙤𝙛 𝙘𝙤𝙙𝙞𝙣𝙜 𝙦𝙪𝙚𝙨𝙩𝙞𝙤𝙣𝙨. 𝙔𝙤𝙪 𝙟𝙪𝙨𝙩 𝙣𝙚𝙚𝙙 𝙩𝙤 𝙪𝙣𝙙𝙚𝙧𝙨𝙩𝙖𝙣𝙙 𝙥𝙖𝙩𝙩𝙚𝙧𝙣𝙨. Most people do this ❌ → Random LeetCode questions Top candidates do this ✅ → Learn patterns → apply everywhere I came across 20 coding patterns and honestly… it simplifies everything Instead of memorizing problems focus on these 👇 ➥ Sliding Window → for subarrays & substrings ➥ Two Pointers → for sorted arrays ➥ Fast & Slow Pointers → cycle detection ➥ Merge Intervals → overlapping ranges ➥ Top K Elements → heap-based problems ➥ Binary Search → optimized searching …and many more patterns like these in doc Don’t ask → 𝗛𝗼𝘄 𝘁𝗼 𝘀𝗼𝗹𝘃𝗲 𝘁𝗵𝗶𝘀 𝗾𝘂𝗲𝘀𝘁𝗶𝗼𝗻? Ask → 𝗪𝗵𝗶𝗰𝗵 𝗽𝗮𝘁𝘁𝗲𝗿𝗻 𝗶𝘀 𝘁𝗵𝗶𝘀? Once you identify the pattern, the solution becomes much easier. Stop solving randomly. Start learning patterns. That’s the real shortcut Doc Credit - Design Gurus ♻️ Repost if you found this useful 🤝 Follow Sattari Sateesh Kumar for more 👨💻 For 1:1 guidance → https://topmate.io/sateesh #python #pyspark #pysparklearning #dataengineering #sqllearning #dataengineeringinterview #azuredataengineer #bigdata #spark #datalearning #datacareer #azuredataengineering #dataengineeringjobs #linkedinlearning #dataengineeringlearning
To view or add a comment, sign in
Explore related topics
- Approaches to Array Problem Solving for Coding Interviews
- Java Coding Interview Best Practices
- Key DSA Patterns for Google and Twitter Interviews
- Tips for Coding Interview Preparation
- Common Algorithms for Coding Interviews
- Common Coding Interview Mistakes to Avoid
- LeetCode Array Problem Solving Techniques
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
Good work bro 🤞