Day 80: The Vertical Flip 🔄 Problem 3643: Flip Square Submatrix Vertically Today’s task was all about in-place matrix manipulation. The goal: flip a k×k submatrix vertically within a larger grid, starting from a given (x,y) coordinate. The Strategy: • Two-Pointer Logic: Instead of creating a copy, I used a row-swapping approach. By iterating only halfway through the submatrix rows (k/2), I could swap the top rows with the bottom ones. • Coordinate Mapping: Carefully mapped the indices so that for every row i, its "mirror" row was x+k−1−(i−x). • In-Place Efficiency: This approach keeps the space complexity at O(1) while the time complexity stays a lean O(k²), making it as fast as physically possible to touch every element. Directly manipulating the grid is always more satisfying than allocating extra memory. It’s all about those clean, symmetrical swaps. 🚀 #LeetCode #Java #MatrixManipulation #Algorithms #ProblemSolving #DailyCode
Flipping Submatrix Vertically in Java
More Relevant Posts
-
Day 24 of my #30DayCodeChallenge: The Art of In-Place Transformation! The Problem: Rotate Image (90 Degrees Clockwise) How do you rotate an n × n matrix without using extra space? The challenge isn't just the rotation- it's doing it in-place, maintaining O(1) extra space complexity. The Logic: Instead of trying to move every element to its final destination in one leap, this problem is best solved by breaking it down into two simple geometric transformations: 1. The Transpose: First, I flipped the matrix over its main diagonal. This turns all rows into columns (and vice versa). Mathematically, we swap matrix [i][j] with matrix [j][i]. 2. The Reflection: Once transposed, the image "sideways." To fix the orientation for a clockwise rotation, I ersed each row. This horizontal flip move columns into their correct 90-dearee positions. One step closer to mastery. Onward to Day 25! #Java #Algorithms #DataStructures #Matrix #ProblemSolving #150DaysOfCode #SoftwareEngineering #LeetCode
To view or add a comment, sign in
-
-
Day 99: Square Root Decomposition & Prefix Multiplications ⚡ Problem 3655: XOR After Range Multiplication Queries II Yesterday’s brute force approach hit a wall today with a TLE (Time Limit Exceeded). The constraints were significantly tighter, requiring a more sophisticated optimization. The Strategy: Square Root Decomposition To handle the queries efficiently, I split the problem based on the step size k: • Large Steps (k ≥ √N): For large gaps, the number of updates is small enough that direct simulation still works within time limits. • Small Steps (k < √N): This is where the magic happens. For small k, I used a Difference Array technique modified for multiplications. • Modular Inverse & Prefix Products: Instead of updating every index, I marked the start (L) and end (R) of the range. I used modInverse to "cancel out" the multiplication after the range ended. A final prefix product pass (jumping by k) applied all updates in O(N) time. Technical Highlights: • Fermat's Little Theorem: Used modPow(x, MOD - 2) to calculate the modular inverse for division. • Complexity: Reduced the worst-case runtime from O(Q⋅N) to O((Q+N)√N). One day away from 100, but the focus remains on the problem in front of me. Consistency isn't about the destination; it's about the quality of the journey. 🚀 #LeetCode #Java #Algorithms #DataStructures #SquareRootDecomposition #DailyCode
To view or add a comment, sign in
-
-
Day 84: Slicing the Grid 🔪 Problem 3546: Equal Sum Grid Partition I Today’s challenge was about finding a single straight cut—either horizontal or vertical—that splits a 2D grid into two halves with equal sums. The Strategy: • The "Odd" Exit: First, I calculated the total sum of the grid. If the total is odd, an equal integer split is impossible—immediate exit. • Vertical Scanning: I iterated through the rows, accumulating the sum. If at any point the current sum equals exactly half of the total, we’ve found a valid horizontal cut. • Horizontal Scanning: Repeated the process by iterating through columns to check for a valid vertical cut. • Early Break: Since grid values are positive, if the running sum exceeds the "halfway" mark, there’s no need to keep checking that direction. It’s a great example of how a simple O(M⋅N) scan can solve a partitioning problem without needing complex recursion or 2D prefix sums. 🚀 #LeetCode #Java #Algorithms #ProblemSolving #DailyCode
To view or add a comment, sign in
-
-
🚀 Day 557 of #750DaysOfCode 🚀 🔥 Solved: XOR After Range Multiplication Queries II (Hard) Today’s problem really pushed my understanding of optimization and patterns in arrays. At first glance, it looks like a simple simulation—but with constraints up to 10⁵, a brute-force approach quickly breaks down. 💡 Key Learnings: Handling range updates efficiently is crucial Observed how step-based traversal (k jumps) affects time complexity Importance of modular arithmetic in large computations XOR properties helped in deriving the final result efficiently ⚡ Challenge: Each query updates elements at intervals, making it tricky to optimize without directly iterating every time. 🧠 Takeaway: Hard problems are less about coding and more about recognizing patterns and optimizing smartly. Consistency is the real game 💯 #LeetCode #DataStructures #Algorithms #CodingJourney #ProblemSolving #Java #100DaysOfCode #KeepGrinding
To view or add a comment, sign in
-
-
𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝: 𝐃𝐞𝐭𝐞𝐫𝐦𝐢𝐧𝐞 𝐖𝐡𝐞𝐭𝐡𝐞𝐫 𝐌𝐚𝐭𝐫𝐢𝐱 𝐂𝐚𝐧 𝐁𝐞 𝐎𝐛𝐭𝐚𝐢𝐧𝐞𝐝 𝐁𝐲 𝐑𝐨𝐭𝐚𝐭𝐢𝐨𝐧 (𝟏𝟖𝟖𝟔) Today I solved an interesting matrix problem that involves matrix rotation and comparison. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦: Given two n × n binary matrices mat and target, determine whether mat can become target by rotating it in 90° increments (0°, 90°, 180°, 270°). 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Check if the current matrix equals the target. If not, rotate the matrix 90° clockwise. Repeat the process up to 4 rotations. If any rotation matches the target → return true. 𝐇𝐨𝐰 𝐫𝐨𝐭𝐚𝐭𝐢𝐨𝐧 𝐰𝐨𝐫𝐤𝐬: Step 1: Transpose the matrix (swap mat[i][j] with mat[j][i]) Step 2: Reverse every row This efficiently performs a 90° clockwise rotation in-place. Time Complexity: O(4 × n²) → effectively O(n²) Space Complexity: O(1) (in-place rotation) 𝐊𝐞𝐲 𝐭𝐚𝐤𝐞𝐚𝐰𝐚𝐲: Matrix rotation problems can often be solved efficiently using the transpose + reverse technique, which is a powerful pattern for many 2D array problems. #LeetCode #DataStructures #Algorithms #Java #CodingPractice #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 85: Leveling Up on Grid Partitions 🧩 Problem 3548: Equal Sum Grid Partition II Today was a grind, but a rewarding one. I tackled the "Hard" version of yesterday’s partition problem. This time, a simple cut wasn't enough—I had to determine if removing exactly one element could balance the two resulting halves. The Strategy: • Frequency Tracking: Standard sums weren't enough. I implemented frequency arrays to track exactly which values existed in each partition. This allowed for O(1) lookups to see if a "balancing" value was available. • The "Diff" Logic: At every potential cut (horizontal or vertical), I calculated the difference between the two halves. ∘ If diff > 0, I searched the "heavy" side for a value equal to the difference to remove. ∘ If diff < 0, I searched the "light" side (which is technically heavier in total weight) for the absolute difference. • Constraint Management: Handled strict boundary rules to ensure that removing an element wouldn't leave a partition disconnected. Solving a Hard-tagged problem after a long debugging session is the best kind of "aha!" moment. It’s a reminder that consistency pays off—yesterday's "Medium" was the foundation for today's breakthrough. 🚀 #LeetCode #Java #Algorithms #DataStructures #ProblemSolving #DailyCode
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟳𝟵/𝟭𝟬𝟬 — 𝗙𝗹𝗶𝗽𝗽𝗶𝗻𝗴 𝗮𝗻 𝗜𝗺𝗮𝗴𝗲 Day 79. Two-pointer for image manipulation. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ #𝟴𝟯𝟮: Flipping an Image (Easy) 𝗧𝗵𝗲 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: Flip each row of a binary image horizontally, then invert it (0 → 1, 1 → 0). Example: [[1,1,0],[1,0,1],[0,0,0]] → [[1,0,0],[0,1,0],[1,1,1]] Two operations. One pass. 𝗧𝗵𝗲 𝗜𝗻𝘀𝗶𝗴𝗵𝘁: Combine flip and invert in the same swap. Two pointers from both ends: Swap and invert simultaneously 1 - value flips the bit No need for two passes 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: For each row: 👉 Two pointers: left and right 👉 Swap positions while inverting: temp = 1 - image[i][left] 👉 image[i][left] = 1 - image[i][right] 👉 image[i][right] = temp Time: O(m × n), Space: O(1) in-place 𝗪𝗵𝘆 𝗜𝘁 𝗠𝗮𝘁𝘁𝗲𝗿𝘀: Two-pointer pattern from Day 31 (linked list reversal). Same concept, different data structure. One pass beats two. Combining operations beats separating them. 𝗖𝗼𝗱𝗲: https://lnkd.in/gkuyyykP 𝗗𝗮𝘆 𝟳𝟵/𝟭𝟬𝟬 ✅ 𝟳𝟵 𝗱𝗼𝘄𝗻. 𝟮𝟭 𝘁𝗼 𝗴𝗼. 𝗙𝗶𝗻𝗮𝗹 𝘀𝘁𝗿𝗲𝘁𝗰𝗵. #100DaysOfCode #LeetCode #TwoPointer #Matrix #Algorithms #InPlaceAlgorithm #CodingInterview #Programming #Java #BitManipulation #AlmostDone
To view or add a comment, sign in
-
Day 89: Lane-Based Swapping 🏎️ Problem 2840: Check if Strings Can be Made Equal With Operations II Yesterday’s problem was a fixed-length vibe check; today, the constraints opened up. The mission: can we sync two strings of any length by swapping characters at any even-to-even or odd-to-odd indices? The Logic: • Two Separate Lanes: Characters at even indices can never move to odd positions. They live in two parallel universes. • Frequency over Simulation: Instead of actually swapping, I just checked if the "pool" of characters in each lane matched between both strings. • The Check: If the count of 'a'-'z' at all even positions in s1 matches s2, and the same holds for odd positions, any arrangement is reachable. It’s a classic case of seeing past the "swapping" distraction and realizing it’s just a frequency distribution problem. One more day down, logic still sharp. 🚀 #LeetCode #Java #Algorithms #DataStructures #DailyCode
To view or add a comment, sign in
-
-
Day 105: Circular Arrays & Shortest Paths 🔄 Problem 2515: Shortest Distance to Target String in a Circular Array Today’s challenge involved finding the minimum steps to reach a target string in a circular array, allowing movement in both directions. The Strategy: • Bidirectional Search: Since the array is circular, the distance can be calculated in two ways: moving forward or moving backward. • Modular Arithmetic: I used (dist + n) % n to handle the wrap-around logic seamlessly, ensuring the index stays within bounds regardless of the direction. • Optimization: By iterating once through the array and comparing the distances for every occurrence of the target, I maintained an O(N) time complexity. Sometimes the most elegant way to handle a "circular" problem is simply embracing the symmetry of the path. 🚀 #LeetCode #Java #Algorithms #ProblemSolving #DailyCode
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