Day 47 of Daily DSA 🚀 Solved LeetCode 74: Search a 2D Matrix ✅ Problem: Given a sorted 2D matrix where: • Each row is sorted • First element of each row > last element of previous row Find whether a target exists in the matrix. Approach: Used an optimized staircase search (top-right traversal). Steps: Start from top-right corner If element == target → return true If element > target → move left If element < target → move down Continue until found or out of bounds ⏱ Complexity: • Time: O(n + m) • Space: O(1) 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 43.84 MB Sometimes choosing the right starting point (top-right) makes the search super efficient 💡 #DSA #LeetCode #Java #Matrix #BinarySearch #CodingJourney #ProblemSolving
Search a 2D Matrix LeetCode Solution
More Relevant Posts
-
Day 48 of Daily DSA 🚀 Solved LeetCode 48: Rotate Image ✅ Problem: Given an n x n matrix, rotate the image by 90° clockwise — in-place (without using extra space). Approach: Used a two-step transformation: Transpose the matrix Reverse each row Steps: Traverse upper triangle and swap → matrix[i][j] ↔ matrix[j][i] For each row: Use two pointers (left, right) Swap elements to reverse the row Matrix gets rotated in-place ⏱ Complexity: • Time: O(n²) • Space: O(1) 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 43.56 MB In-place transformations are powerful — no extra space, just smart manipulation 💡 #DSA #LeetCode #Java #Matrix #Arrays #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 45/60 – DSA Challenge Today’s problem was about searching in a Rotated Sorted Array using Binary Search 🔍 🔍 Problem Solved: Given a rotated sorted array, efficiently find the index of a target element. 🧠 Approach I Used: Instead of directly applying binary search, I broke the problem into two steps: 1️⃣ Find the pivot (rotation point) using binary search 2️⃣ Apply binary search on the correct half of the array Left half → sorted from start to pivot-1 Right half → sorted from pivot to end ⚡ Key Insight: By identifying the pivot, the problem becomes two simple binary searches Careful boundary checks (like target >= nums[0]) ensure we search in the correct half Handling edge cases (like pivot at index 0) is crucial for correctness 📈 Complexity: Time: O(log n) Space: O(1) 🎯 What I Learned: Complex problems can often be simplified by breaking them into smaller parts Binary Search is not just about searching—it’s about understanding structure Boundary conditions can make or break your solution Debugging edge cases today made the concept even clearer 💡 #Day45 #DSAChallenge #BinarySearch #Java #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🔥 Day 364 – Daily DSA Challenge! 🔥 Problem: 🎯 Find K Closest Elements Given a sorted array arr, an integer k, and a target x, return the k closest elements to x in ascending order. 💡 Key Insight — Binary Search on Window Instead of checking each element, we binary search the starting index of a window of size k. Search space: Possible windows → [0 ... n - k] 🧠 Decision Logic At index mid, compare: Distance of left boundary → x - arr[mid] Distance of right boundary → arr[mid + k] - x We choose direction based on: ⚡ Algorithm Steps ✅ Initialize: left = 0, right = n - k ✅ Binary search: If left side is farther → shift right Else → move left ✅ Final window starts at left ✅ Collect k elements ⚙️ Complexity ✅ Time Complexity: O(log(n - k) + k) (binary search + result building) ✅ Space Complexity: O(k) 💬 Challenge for you 1️⃣ Why do we search on window positions instead of elements? 2️⃣ Can you solve this using a heap approach? 3️⃣ What if array was unsorted? #DSA #Day364 #LeetCode #BinarySearch #SlidingWindow #Arrays #Java #ProblemSolving #KeepCoding
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
-
-
Day 8/30 — DSA Challenge 🚀 Problem: Search a 2D Matrix II Topic: Matrix + Binary Search Pattern Difficulty: Medium Approach: Started from top-right corner of the matrix If current element == target → return true If current element < target → move down If current element > target → move left Mistake / Challenge: Initially tried applying binary search like previous problem (LeetCode 74) Realized matrix is not globally sorted, so that approach fails Fix: Used optimal “staircase search” approach Reduced time complexity efficiently Key Learning: Not all sorted matrices can be treated as 1D arrays Recognize pattern: row-wise + column-wise sorted → use pointer approach Time Taken: 45 minutes Consistency check ✅ See you on Day 9. GitHub Repo: https://lnkd.in/gHW9vKUf #DSA #LeetCode #Java #Matrix #BinarySearch #LearningInPublic
To view or add a comment, sign in
-
-
Day 51 of Daily DSA 🚀 Solved LeetCode 83: Remove Duplicates from Sorted List ✅ Problem: Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Approach: Used a single pointer traversal since the list is already sorted, making duplicate detection straightforward. Steps: Start from head node Compare current node with next node If values are same → skip next node Else move current pointer forward Continue until end of list Return updated head ⏱ Complexity: • Time: O(n) • Space: O(1) 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 45.48 MB Sorted input often simplifies the logic — duplicates become adjacent and easy to remove. #DSA #LeetCode #Java #LinkedList #CodingJourney #ProblemSolving #Algorithms
To view or add a comment, sign in
-
-
Day 41 of Daily DSA 🚀 Solved LeetCode 20: Valid Parentheses ✅ Problem: Given a string containing only (), {}, [], determine if the input string is valid. Rules: Open brackets must be closed by the same type Open brackets must be closed in the correct order Every closing bracket must have a matching opening bracket Approach: Used a Stack to track opening brackets and validate matching pairs. Steps: Traverse the string Push opening brackets onto the stack For closing brackets → check top of stack If it matches → pop Else → return false At the end, stack should be empty ⏱ Complexity: • Time: O(n) • Space: O(n) 📊 LeetCode Stats: • Runtime: 3 ms (Beats 87.41%) ⚡ • Memory: 43.37 MB A classic stack problem that builds strong fundamentals for expression parsing & validation. #DSA #LeetCode #Java #Stack #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 36 of my DSA Journey 📌 Problem: 560. Subarray Sum Equals K Given an array and a value k, we need to find how many subarrays (continuous parts of the array) add up to k. 💡 My Approach (Simple Explanation): Instead of checking all subarrays (which is slow), I used the prefix sum + HashMap trick. Keep adding elements to get a running sum Check if (current sum - k) was seen before If yes, it means a valid subarray exists Store prefix sums in a map to reuse quickly This makes the solution efficient ⚡ ✅ Result: Accepted ⏱ Runtime: 0 ms ✨ Small optimizations like this really show how powerful concepts can simplify problems! #Day36 #DSA #CodingJourney #Java #LeetCode #ProblemSolving
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 27 Today’s focus: Binary Search on modified arrays. Problem solved: • Search in Rotated Sorted Array (LeetCode 33) Concepts used: • Binary Search • Identifying sorted halves • Conditional search space reduction Key takeaway: This problem extends binary search to a rotated sorted array, where the array is not fully sorted but divided into two sorted parts. At each step, we: • Find the mid element • Check which half (left or right) is sorted • Decide whether the target lies in the sorted half • Eliminate the other half This allows us to still achieve O(log n) time complexity. Continuing to strengthen fundamentals and problem-solving consistency. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
Day 39 of Daily DSA 🚀 Solved LeetCode 1446: Consecutive Characters ✅ Problem: The power of a string is the maximum length of a non-empty substring that contains only one unique character. Given a string s, return its power. Rules: * Substring must be non-empty * Substring must contain only one unique character * Return the maximum such length Approach: Used a simple linear scan to track the current streak of consecutive identical characters and update the maximum. Steps: 1. Initialize max and count both to 1 2. Iterate from index 1 onwards 3. If current character equals previous → increment count 4. Else → reset count to 1 5. Update max at every step 6. Return max ⏱ Complexity: • Time: O(n) • Space: O(1) 📊 LeetCode Stats: • Runtime: 29 ms (Beats 2.02%) • Memory: 45.33 MB Sometimes the simplest sliding window — just two variables — is all you need to solve a problem cleanly. #DSA #LeetCode #Java #SlidingWindow #Strings #CodingJourney #ProblemSolving
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