🚀 Day 7 of #100DaysOfCode Solved Search a 2D Matrix on LeetCode using an optimized matrix traversal approach 🔎 🧠 Key insight: Since rows and columns are sorted, starting from the top-right corner helps eliminate either a row or a column in each step. ⚙️ Approach: 🔹Start at top-right element 🔹If value < target → move down 🔹If value > target → move left 🔹Repeat until target is found or bounds are crossed ⏱️ Time Complexity: O(m + n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #Matrix #Java #ProblemSolving #LearningInPublic #CodingJourney
Optimized Matrix Traversal for 2D Search on LeetCode
More Relevant Posts
-
🚀 Day 20 of #100DaysOfCode Solved 54. Spiral Matrix on LeetCode 🌀 🧠 Key insight: Spiral traversal is all about maintaining boundaries. By shrinking the top, bottom, left, and right limits after each pass, we can cover every element exactly once. ⚙️ Approach: 🔹Maintain four pointers: top, down, left, right 🔹Traverse: 🔹Left → Right (top row) 🔹Top → Bottom (right column) 🔹Right → Left (bottom row) 🔹Bottom → Top (left column) 🔹Update boundaries after each traversal ⏱️ Time Complexity: O(m × n) 📦 Space Complexity: O(1) (excluding output list) #100DaysOfCode #LeetCode #DSA #Matrix #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
#Day58 of my second #100DaysOfCode Today’s problem was about carefully handling overlapping intervals. DSA • Solved Merge Intervals (LeetCode 56) • Implemented a brute force approach by checking and merging overlapping intervals → O(2n) + O(n log n) time • Implemented the optimal approach by sorting intervals and merging them in a single traversal → O(n log n) + O(n) time • Key idea: once intervals are sorted by start time, overlapping ranges can be merged efficiently in one pass These interval problems really test attention to detail with edge cases. #DSA #Algorithms #LeetCode #Java #100DaysOfCode #WomenWhoCode #BuildInPublic #LearningInPublic
To view or add a comment, sign in
-
-
🚀 #100DaysOfCode | Day 41 💻 LeetCode – Add to Array-Form of Integer Today I solved a problem focused on array manipulation and digit-by-digit addition. 🔎 Problem: An integer is given in array form, where each element represents a digit. The task is to return the array representation of num + k. 💡 Approach: Instead of converting the array into a number, I simulated manual addition from the last digit, just like we do on paper. ✔ Traverse the array from right to left ✔ Add digits with k and manage the carry ✔ Store the result digits and reverse at the end This approach efficiently handles large inputs and avoids integer overflow. Small problems like this strengthen logic building and problem-solving skills. #LeetCode #DSA #100DaysOfCode #ProblemSolving #Java #CodingJourney #SoftwareDeveloper #TechGrowth
To view or add a comment, sign in
-
-
🚀 Day 523 of #750DaysOfCode 🚀 📌 Problem Solved: Check if Binary String Has at Most One Segment of Ones (LeetCode 1784) 🔎 Problem Summary: Given a binary string s (without leading zeros), we need to check whether the string contains at most one contiguous segment of 1s. If the 1s appear in more than one separate segment, we return false. (LeetCode) 💡 Key Insight: Since the string starts with 1, the only valid structure is: 111...000... If we ever encounter the pattern "01", it means a 1 appeared again after a 0, which creates multiple segments of ones. (AlgoMonster) ⚙️ Approach: Traverse the string or simply check if "01" exists. If "01" is found → more than one segment → return false. ⏱ Complexity: Time: O(n) Space: O(1) 📚 Takeaway: Sometimes string problems become much easier when we identify patterns instead of counting segments. Consistency > Motivation. On to Day 524 tomorrow. 💪 #LeetCode #Java #DataStructures #Algorithms #CodingChallenge #Consistency #ProblemSolving #100DaysOfCode #750DaysOfCode
To view or add a comment, sign in
-
-
Day 27/100 – LeetCode Challenge 🚀 Problem: Sort List Approach: Applied Merge Sort on the linked list Used slow and fast pointers to find the middle Recursively sorted both halves Merged the sorted halves Time Complexity: O(n log n) Space Complexity: O(log n) (recursion stack) Key takeaway: Merge sort is the most efficient sorting technique for linked lists, as it avoids random-access operations required by other algorithms. #LeetCode #100DaysOfCode #DSA #Java #LinkedList #MergeSort #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 44 Out of #365DayOfCode - LeetCode Today I worked on the Set Matrix Zeroes problem, where the goal is to modify a 2D matrix such that if any element is 0, its entire row and column are set to 0. I implemented an efficient solution with O(m × n) time complexity and optimized space usage by carefully handling matrix traversal and edge cases. This problem helped me strengthen my understanding of: 2D array manipulation In-place updates Optimizing space complexity Handling edge cases in algorithms Consistent practice is helping me improve my problem-solving skills step by step. 🚀 #DSA #Java #ProblemSolving #CodingPractice #365DaysOfCode Github link: https://lnkd.in/gGUy_MKZ
To view or add a comment, sign in
-
-
Most problems aren’t about brute force. They’re about recognizing patterns. Today’s focus: Day 48/100 – Sliding Window Mastery 🚀(Longest Subarray with At Most 2 Distinct Elements) Instead of checking every possible subarray (O(n²)), I used the Sliding Window + HashMap approach to optimize it to O(n) time complexity. 🔹 Expand the window using r 🔹 Track frequencies using HashMap 🔹 Shrink from l when distinct elements exceed 2 🔹 Update maximum length dynamically Clean. Efficient. Scalable. Consistent practice is making pattern recognition faster and more intuitive every day. #Day48 #100DaysOfCode #DSA #Java #CodingJourney #SlidingWindow #ProblemSolving #LeetCode
To view or add a comment, sign in
-
-
🚀 Day 23/100 – LeetCode Challenge Today’s problem: Partitioning Into Minimum Number of Deci-Binary Numbers 🔹 Key Insight: The minimum number of deci-binary numbers required is equal to the maximum digit present in the string. 🔹 Approach: Traverse through each character in the string Convert it to integer (ch - '0') Track the maximum digit Return the maximum value 🔹 Time Complexity: O(n) 🔹 Space Complexity: O(1) ✨ Simple logic, but powerful observation! Instead of constructing numbers, we just analyze the digits. Consistency > Motivation 💪 #Day23 #100DaysOfCode #LeetCode #Java #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🔥 Day 84 of #100DaysOfCode Today’s problem: LeetCode: Find K Closest Elements 🎯 📌 Problem Summary Given: A sorted array arr An integer k A target value x Return the k closest integers to x in the array. The result must be sorted in ascending order. Example: arr = [1,2,3,4,5], k = 4, x = 3 Output → [1,2,3,4] 🧠 Approach: Two Pointers Shrinking Window Since the array is already sorted, we can: Start with: l = 0 r = arr.length - 1 While the window size is bigger than k: Compare distances: |x - arr[l]| |x - arr[r]| Remove the element that is farther from x ⚙️ Core Idea: while (r - l >= k): if left is closer: r-- else: l++ Finally, return elements from l to r. ⏱ Time Complexity: O(n - k) 💾 Space Complexity: O(1) (excluding result list) 🚀 Performance ⚡ Runtime: 4ms (98% faster) Solid optimization using the sorted property of the array. 💡 What I Learned When array is sorted → always think Two Pointers. Instead of building result, shrink unnecessary elements. Cleaner logic often gives better performance. Consistency > Motivation. On to Day 85 🔥 #100DaysOfCode #LeetCode #TwoPointers #Java #DSA #CodingJourney #InterviewPrep
To view or add a comment, sign in
-
-
Approached this problem using recursion by traversing both left and right subtrees and keeping track of the maximum depth encountered. The idea is simple: Depth of a tree = 1 + max(depth of left, depth of right) This problem strengthened my understanding of tree traversal and recursive thinking. Consistent practice with tree problems to improve problem-solving skills step by step 🚀 #LeetCode #DataStructures #Java #BinaryTree #DSA #CodingJourney
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