Day 61: Grid Lock 🧩 Problem 1536: Minimum Swaps to Arrange a Binary Grid Today was about cleaning up a binary grid to satisfy a specific condition: all elements above the main diagonal must be zero. The catch? You can only swap adjacent rows. I tackled this by first converting each row into a single integer representing its count of trailing zeros. This turned a complex 2D grid problem into a 1D greedy search. For each position in the grid, I looked for the first available row that met the zero-count requirement, added the "distance" to my swap total, and removed that row from the pool. It’s essentially a selection sort logic where you only care about the trailing zeros. If you hit a point where no row in the remaining list fits the bill, it’s an immediate -1. Clean, greedy, and efficient enough to keep the streak rolling. 🚀 #LeetCode #Java #GreedyAlgorithms #Matrix #Coding #DailyCode
Binary Grid Minimum Swaps with Java
More Relevant Posts
-
100 Days of Code Day-14 Solved a classic problem: Longest Common Prefix Given an array of strings, the task is to find the longest common prefix shared among them. If no common prefix exists, return an empty string. Approach: Used a simple horizontal scanning method: Start with the first string as the prefix Compare it with each string Gradually reduce the prefix until it matches all strings Example: "flower", "flow", "flight" → "fl" "dog", "racecar", "car" → "" A good exercise to strengthen string handling and logical thinking. #Java #DSA #ProblemSolving
To view or add a comment, sign in
-
-
Day 44 of DSA 🚀 Solved Minimum Number of Flips to Make the Binary String Alternating. 💡 Key Insight: An alternating binary string can only follow two patterns: 010101... 101010... To handle rotations efficiently: Double the string (s + s) Use a sliding window of size n Count mismatches with both patterns Track the minimum flips required. ⏱ Time: O(n) 💾 Space: O(n) Good practice for sliding window + pattern matching. #DSA #Java #LeetCode #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 LeetCode Problem Solved: Longest Repeating Character Replacement (Problem #424) I recently solved this medium-level sliding window problem, which focuses on optimizing substring length under constraints. 💡 Approach Used: Applied Sliding Window Technique Maintained a frequency array to track character counts Tracked the maximum frequency within the window Adjusted the window dynamically to ensure valid replacements ⚡ Complexity: Time Complexity: O(n) Space Complexity: O(1) (constant space for 26 uppercase letters) 📊 Performance: Runtime: 7 ms (Beats 89.78%) Memory: 46.12 MB ✨ This problem helped me strengthen my understanding of window optimization techniques and how to efficiently handle constraints in strings. #LeetCode #DSA #SlidingWindow #ProblemSolving #Java #CodingJourney #TechGrowth
To view or add a comment, sign in
-
-
Day 25/30 – LeetCode streak Problem: Complement of Base 10 Integer You need to flip all bits in 'n'’s binary representation, but only up to its most significant '1' (no leading zeros). Core idea * The complement should only flip bits within the bit-length of 'n'. For example, '5' is '101₂'; its complement is '010₂' = '2', not some infinite stream of ones. * Build a mask of the form '111...1' that has the same length as 'n' in binary. Then 'n ^ mask' flips exactly those bits. * Special case: if 'n == 0', binary is "0" and complement is "1", so return '1'. Day 25 takeaway: This is a clean bitmask + XOR pattern: instead of flipping bits one-by-one, construct a full '111...1' mask up to the MSB and XOR once to get the complement. #leetcode #dsa #java #bitmanipulation #consistency
To view or add a comment, sign in
-
-
Day 88: Swap Bubbles 🫧 Problem 2839: Check if Strings Can be Made Equal With Operations I Today was a quick O(1) vibe check. The task: can we sync up two strings of length 4 by only swapping characters that are 2 units apart? The Breakdown: • Bubbled Logic: Since we only swap i and i+2, indices 0 and 2 are in their own world. Same for 1 and 3. They don't mix. • The "Match or Swap" Check: For each pair, they either already fit the target or they need exactly one swap to get there. If neither works, it’s a "GG." • The Result: No sorting, no heavy lifting—just a clean, symmetrical comparison to keep the streak alive. After the "Hard" problems drain the brain, these "Easy" ones are a nice reminder that the logic is still sharp. We keep moving. 🚀 #LeetCode #Java #StringAlgorithms #ProblemSolving #DailyCode
To view or add a comment, sign in
-
-
🚀 Day 14/100 – #100DaysOfCode Challenge 🔍 Problem Solved: Single Number (LeetCode 136) Today’s problem was about finding the element that appears only once in an array where every other element appears twice. 💡 Key Insight: Used the power of XOR (^) bit manipulation Same numbers cancel out → a ^ a = 0 XOR with 0 gives the number → a ^ 0 = a 👉 By XOR-ing all elements, duplicates vanish, leaving the unique number! ⚡ Approach: Traverse the array once Apply XOR on each element Result = single number ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) 🔥 What I Learned: Bit manipulation can simplify problems drastically XOR is super powerful for pairing problems #Day14 #CodingChallenge #Java #DataStructures #Algorithms #BitManipulation #LeetCode #100DaysOfCode #CodingJourney
To view or add a comment, sign in
-
-
Day 25/75 — Longest Repeating Character Replacement Today's problem involved an important sliding window pattern. Goal: Find the longest substring where we can replace at most k characters so that all characters become the same. Key Idea: If the window length is L and the most frequent character appears maxFreq times, then: replacements needed = L - maxFreq The window is valid if: L - maxFreq ≤ k Using this rule, we expand and shrink the sliding window to find the maximum valid length. Time Complexity: O(n) Space Complexity: O(1) This problem helped reinforce how sliding window techniques can efficiently solve substring problems. 25/75 — continuing the DSA journey 🚀 #Day25 #DSA #SlidingWindow #Java #Algorithms #LeetCode
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 15 Today’s focus: Circular array handling with Sliding Window. Problem solved: • Defuse the Bomb (LeetCode 1652) Concepts used: • Sliding Window technique • Circular array traversal • Modulo indexing Key takeaway: This problem involves computing the sum of the next or previous k elements for each index in a circular array. Instead of recalculating the sum for every position, a sliding window can be used to maintain the sum efficiently. For positive k, we consider the next k elements; for negative k, we consider the previous |k| elements. Using modulo (% n) helps simulate circular behavior without actually modifying the array. As the window moves, we: • Add the incoming element • Remove the outgoing element This allows updating the sum in O(1) time per step, resulting in an overall O(n) solution. This problem highlights how sliding window can be adapted to handle circular structures with careful indexing. Continuing to strengthen problem-solving consistency and pattern recognition. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
💡 Day 37 of LeetCode Problem Solved! 🔧 🌟75. Sort Colors🌟 Task : • Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue. • We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively. • You must solve this problem without using the library's sort function. Example 1: Input: nums = [2,0,2,1,1,0] Output: [0,0,1,1,2,2] Example 2: Input: nums = [2,0,1] Output: [0,1,2] #LeetCode #Java #DSA #ProblemSolving #Consistency #100DaysOfChallenge #CodingJourney #KeepGrowing
To view or add a comment, sign in
-
-
Day 45 on LeetCode Largest Submatrix With Rearrangements :- This problem clicked once I stopped treating columns as fixed. Rearranging them turns each row into a controllable histogram. Approach: ->Build vertical heights of consecutive 1s ->Sort each row to simulate optimal column order ->Evaluate max area using height × width Time Complexity: O(m × n log n) The editorial on LeetCode made the difference here. It didn’t just give the solution, it clarified the intuition behind sorting heights, which is easy to miss on your own. #DSA #LeetCode #Java
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