✅ LeetCode 3446 – Sort Matrix by Diagonals Successfully solved another interesting matrix manipulation problem! 🧩 Problem Summary: Given an n × n matrix, the task is to sort: The bottom-left diagonals (including the main diagonal) in non-increasing order. The top-right diagonals in non-decreasing order. 💡 Approach: Use a dictionary to group elements by diagonal index (j - i). Sort each diagonal individually based on its position (top or bottom triangle). Reconstruct the matrix by placing back the sorted values. ⚙️ Key Concepts: Matrix traversal Diagonal indexing (j - i) Sorting and reconstruction 📊 Result: ✅ Accepted with 0 ms runtime 💪 Optimized, clean, and easy-to-read solution #LeetCode #Python #ProblemSolving #CodingChallenge #DataStructures #Algorithms #Matrix #DeveloperJourney
Solved LeetCode 3446: Sort Matrix by Diagonals
More Relevant Posts
-
✅ Learned to solve “Remove Duplicates from Sorted Array” (in-place, O(n) time, O(1) space)! Sorted input means every duplicate sits next to its twin—perfect setup for the two-pointer pattern: scan once, write uniques forward, and return the count k while keeping the first k positions clean and ordered. What clicked: - Two pointers: one scans, one writes uniques forward - Skip repeats deterministically thanks to sorting - Edge cases covered: empty array, all duplicates, negatives, mixed ranges Level-ups next: “Remove Duplicates II” (allow at most twice) and “Remove Element” to deepen the pattern muscle. What’s your favorite twist on this technique? 🚀 #LeetCode #TwoPointers #Arrays #InPlace #DSA #Algorithms #InterviewPrep #ProblemSolving #TimeComplexity #CodingChallenge #Python #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 5 of #100DaysOfLeetCode Problem: 54. Spiral Matrix Category: Arrays / Matrix Traversal Today’s challenge focused on traversing a 2D matrix in spiral order and returning all elements in that pattern. This problem was really fun to solve because it required handling multiple edge cases while maintaining clean traversal logic. 🧠 Key Learnings: Used the concept of repeatedly peeling off the outer layer of the matrix. Traversed top row → right column → bottom row → left column in sequence. Understood the importance of checking matrix boundaries after each step to avoid index errors. Improved logical thinking for problems involving nested data structures. 🎯 Takeaway: Matrix traversal problems are all about maintaining control over direction and boundaries — once that’s handled, the logic flows smoothly. #LeetCode #100DaysOfCode #ProblemSolving #CodingJourney #Matrix #Arrays #Python #AIEngineer #Consistency
To view or add a comment, sign in
-
-
🔢 Understanding Selection Sort 🎯 Problem: Given an array of numbers, sort them in ascending order using the Selection Sort algorithm. 🧩 Concept: Selection Sort works by repeatedly finding the minimum element from the unsorted part of the array and placing it at the beginning. 🔹In each iteration, we assume the first element is the smallest. 🔹Then, we find the actual minimum from the remaining unsorted part. 🔹Finally, we swap it with the current index element. 🕒 Time Complexity: O(N^2) - For each element, we search the rest of the array for the smallest element. 💾 Space Complexity: O(1) - Sorting is done in place (no extra memory used). #Python #DataStructures #Algorithms #Sorting #CodingJourney #ProblemSolving #LearningInPublic
To view or add a comment, sign in
-
-
Day 68: Search in 2D Sorted Matrix (Search Space Reduction) 🔎 I'm continuing the streak on Day 68 of #100DaysOfCode with a challenging matrix search problem! The task is to find a target value in an $m \times n$ matrix where both rows and columns are sorted in ascending order. The key to solving this efficiently is Search Space Reduction. Instead of performing a standard search, my solution uses a smart traversal technique: Starting Point: I begin the search at the top-right corner of the matrix. Decision Logic: If the current value equals the target, we stop. If the current value is greater than the target, the entire current column can be eliminated, so we move left. If the current value is less than the target, the entire current row can be eliminated, so we move down. This strategy eliminates one row or one column in every step, guaranteeing an optimal O(m + n) time complexity and O(1) extra space. #Python #DSA #Algorithms #Matrix #Search #100DaysOfCode #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 3 of #100DaysofDSA Today’s focus was on the “Set Matrix Zeroes” problem — a classic array-matrix question that tests both logic and optimization thinking It began with the brute-force idea: storing all zero positions and then marking corresponding rows and columns later. It works but takes O(m × n) time and O(m + n) extra space. Next, then explored a better approach using two auxiliary arrays to track which rows and columns should be zeroed. This improved the clarity but still consumed additional memory and space. Finally, then to reduce the complexity I tackled the optimal solution, which achieves O(1) extra space by using the first row and first column of the matrix itself as markers. A small Boolean flag handles the edge case when the first row contains a zero. This subtle observation transforms the logic completely — turning a memory-heavy method into a clean in-place algorithm. It was a good reminder that optimization isn’t just about speed — it’s about finding elegance in constraints. #100DaysOfDSA #MatrixProblems #Optimization #SpaceComplexity #Python #ProblemSolving
To view or add a comment, sign in
-
-
Day 13 of #100DaysOfLeetCode Problem: 88. Merge Sorted Array Category: Arrays / Sorting / Two Pointers Today’s challenge focused on merging two sorted arrays into one, maintaining the sorted order. It’s a simple yet powerful problem that tests your understanding of array indices, merging logic, and in-place updates. 🧠 Key Learnings: Extended the first array by adding elements from the second, then sorted the combined list. Reinforced the importance of efficient merging techniques for sorted sequences. Understood how in-place operations can reduce memory usage in practical scenarios. Strengthened logic building around sorting fundamentals and index-based insertion. 🎯 Takeaway: Even simple array problems help build strong fundamentals in sorting and data manipulation — the key to mastering larger algorithmic challenges. #LeetCode #100DaysOfCode #ProblemSolving #CodingJourney #Arrays #Sorting #Python #AIEngineer #Consistency
To view or add a comment, sign in
-
-
Day 66: Find Minimum in Rotated Sorted Array II 📉 (Simple Version) I'm tackling an advanced Binary Search problem on Day 66 of #100DaysOfCode: "Find Minimum in Rotated Sorted Array II." The challenge is to find the minimum element in a rotated sorted array that may contain duplicates. My solution uses a modified Binary Search approach. The key difficulty is when the middle element (nums[mid]) equals the rightmost element (nums[right]). In this case, we can't tell if the minimum is on the left or the right. To resolve this ambiguity, I simply discard the rightmost element by setting right -= 1. This adjustment keeps the search on track, achieving an optimal O(log n) complexity in the typical case. My solution achieved 100% runtime efficiency! #Python #DSA #Algorithms #BinarySearch #Arrays #100DaysOfCode #ProblemSolving
To view or add a comment, sign in
-
-
Day 16 of #100DaysOfLeetCode Today’s problem focused on substring counting within binary strings and required an efficient approach to handle potentially large input sizes without generating every substring explicitly. 1. Number of Substrings With Only 1s The task was to count the total number of substrings that consist entirely of the character '1', with the final result taken modulo (10^9 + 7). Instead of constructing the substrings, the key insight is that each continuous block of 1s contributes a predictable number of valid substrings. 🔹 My Approach: Iterated through the string while tracking the current streak length of consecutive 1s. Each time a block ended, computed the number of substrings from that block using the formula: k*(k+1)/2 where k is the length of the streak. Added the total from each block to the final answer, applying the modulo constraint throughout. Completed the process with a final update for any trailing block of 1s. What I Learned: This problem reinforces how recognizing mathematical patterns within sequences can transform a brute-force solution into a simple linear scan. Efficient substring counting often comes down to understanding structure rather than enumerating possibilities. 📊 Complexity Analysis: Time Complexity: O(n) — single pass over the string. Space Complexity: O(1) — constant space approach. #day16 #100daysofleetcode #leetcode #DSA #python #leetcodes #striver
To view or add a comment, sign in
-
-
🚀 LeetCode #1625: Lexicographically Smallest String After Applying Operations Today’s problem was about transforming a numeric string using two operations: adding a value to digits at odd indices and rotating the string, to get the lexicographically smallest result possible. The challenge was to handle infinite possibilities smartly. I used a Breadth First Search (BFS) approach to systematically explore all reachable string states while keeping track of visited ones. 💡 Key Takeaways: - Some problems don’t need a direct formula, they need systematic exploration. - BFS is not just for graphs; it’s a powerful tool for exploring state transitions too. - Modular arithmetic and rotation logic often come together in string manipulation problems. This one was a great reminder that clean logic and state tracking can solve even the most “infinite looking” problems efficiently. #LeetCode #ProblemSolving #Python #DSA #CodingChallenge #Algorithms #BFS #StringManipulation #LearningEveryday
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