Kth Smallest Element in Sorted Matrix LeetCode Solution

🚀 Day 40 of #100DaysOfCode Solved 378. Kth Smallest Element in a Sorted Matrix on LeetCode 🔍📊 🧠 Key Insight: Each row and column in the matrix is sorted, so instead of flattening and sorting the matrix, we can apply Binary Search on the value range. ⚙️ Approach: 1️⃣ Define the search range: 🔹left = smallest element (matrix[0][0]) 🔹right = largest element (matrix[n-1][n-1]) 2️⃣ Perform binary search on values: 🔹Pick mid 🔹Count how many elements in the matrix are ≤ mid 3️⃣ If count ≥ k → move left side (potential answer found) 4️⃣ Else → move right side This efficiently narrows down the kth smallest element. ⏱️ Time Complexity: O(n log(max-min)) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #Matrix #Java #ProblemSolving #InterviewPrep #LearningInPublic

  • graphical user interface, application

To view or add a comment, sign in

Explore content categories