Count Days Without Meetings: LeetCode 3169

🚀 Day 38 of mastering DSA patterns in Java Today I solved a very practical interval problem that combines merging logic + gap counting. 🧠 Pattern: Intervals → Merge + Count Gaps 🔹 Problem solved: LeetCode 3169 – Count Days Without Meetings https://lnkd.in/dnDiPA3M --- 💡 Problem Understanding We are given: • Total number of days • Meeting intervals in the form [start, end] (inclusive) • Meetings may overlap We need to count: ➡️ How many days have NO meetings scheduled? --- 🧠 Core Thinking This is NOT just counting gaps blindly. Because meetings can overlap, we must first handle overlapping ranges correctly. Step-by-step logic: 1️⃣ Sort meetings by start time 2️⃣ Track the latest meeting end seen so far. 3️⃣ For each meeting: If "current start > latestEnd + 1" → There is a gap → Add "(current start - latestEnd - 1)" to free days. Then update: "latestEnd = max(latestEnd, current end)" 4️⃣ After processing all meetings, add the days after the last meeting: "totalDays - latestEnd" --- 🛠 Why this works? Because we are effectively: • Merging overlapping intervals • Counting the gaps between merged intervals So it’s a classic: 👉 Intervals + Greedy + Gap Counting --- ⏱ Time Complexity Sorting → O(n log n) Linear scan → O(n) Total → O(n log n) --- 📌 How to recognize this pattern? If the question asks: • free days • available time • unused slots • count days without events → Think: Sort → Merge → Count gaps --- 🙏 Grateful to my mentor Pratyush Narain for helping me build strong intuition around interval merging logic. 📚 Big lesson of the day: Most “free time” problems are just merged intervals + gap calculation in disguise. Have you solved any gap-based interval problems recently? 👇 #DSA #KadanesAlgorithm #DynamicProgramming #ProblemSolving #DataStructures #Algorithms #CodingJourney #LeetCode #LearningInPublic #BTechCSE #SoftwareEngineering #PlacementsPrep #CodingPractice #TechCareer #Consistency

  • text

To view or add a comment, sign in

Explore content categories