🚀 Day 96 of My 100 Days LeetCode Challenge | Java Today’s problem was a great combination of matrix manipulation and optimization thinking. The challenge was to find the largest submatrix consisting of only 1’s, where we are allowed to rearrange the columns in each row. At first, it looks like a standard matrix problem, but the twist is the column rearrangement, which changes how we approach it. Instead of checking fixed columns, we treat each row like a histogram and sort column heights to maximize the possible area. By building heights of consecutive 1’s and then sorting each row, we can efficiently compute the maximum possible rectangle area. ✅ Problem Solved: Largest Submatrix With Rearrangements ✔️ All test cases passed (59/59) ⏱️ Runtime: 13 ms 🧠 Approach: Matrix + Greedy + Sorting 🧩 Key Learnings: ● Transforming a matrix into histogram heights simplifies the problem. ● Sorting can help maximize outcomes when order is flexible. ● Greedy strategies work well when rearrangements are allowed. ● Small problem twists (like column swaps) can completely change the approach. ● Thinking in terms of heights and widths helps in area-based problems. This problem was a reminder that changing perspective (matrix → histogram) can make complex problems much simpler. 🔥 Day 96 complete — improving matrix problem-solving and optimization skills. #LeetCode #100DaysOfCode #Java #Matrix #Greedy #Algorithms #ProblemSolving #DSA #CodingJourney #Consistency
Largest Submatrix With Rearrangements in Java
More Relevant Posts
-
🚀 Day 536 of #750DaysOfCode 🚀 Today I solved Count Submatrices With Equal Frequency of X and Y (LeetCode 3212) using Java. 🔹 Problem Summary: Given a grid containing 'X', 'Y', and '.', we need to count the number of submatrices starting from the top-left corner (0,0) such that: • The number of 'X' and 'Y' is equal • The submatrix contains at least one 'X' 🔹 Approach: Instead of checking every possible submatrix (which would be too slow), I used the Prefix Sum technique. I maintained two prefix matrices to store the count of 'X' and 'Y' up to each cell. For every position (i, j), I checked: countX == countY and countX > 0 If true, that submatrix is valid. This reduces the complexity to O(n × m), which works efficiently for large grids. 🔹 Key Concepts Learned: ✅ Prefix Sum in 2D ✅ Matrix traversal optimization ✅ Handling constraints up to 1000 × 1000 ✅ Clean implementation in Java Consistent practice is making problem-solving faster and more structured every day. #750DaysOfCode #Day536 #LeetCode #Java #DataStructures #Algorithms #PrefixSum #CodingChallenge #ProblemSolving
To view or add a comment, sign in
-
-
Day 93 - LeetCode Journey Solved LeetCode 9: Palindrome Number in Java ✅ At first glance, it feels like a string problem… but the real challenge is solving it without converting to string. Instead of reversing the whole number, I reversed only half of it and compared both parts. This avoids overflow and keeps it efficient. Smart approach > brute force 💡 Key takeaways: • Handling edge cases (negative numbers, trailing zeroes) • Reversing only half of the number • Avoiding extra space (no string conversion) • Writing optimized mathematical logic ✅ All test cases passed ⚡ O(log n) time and O(1) space Sometimes the best solutions are the simplest ones, just a different way of thinking 🔥 #LeetCode #DSA #Java #Math #ProblemSolving #CodingJourney #InterviewPrep #Consistency #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 72 — LeetCode Practice 🚀 Today’s problem: Sorting the Sentence 💡 What I learned today: • Practiced string manipulation and splitting techniques • Understood how to extract numbers from characters using ASCII logic • Learned to map words to correct positions using indexing • Improved attention to detail while handling edge cases 🧠 Key idea: Each word has a number at the end → use it to place the word in the correct position → then remove the number and rebuild the sentence. ✨ Consistency is slowly turning confusion into clarity. #Day72 #LeetCode #Java #ProblemSolving #DSA #CodingJourney #Consistency
To view or add a comment, sign in
-
-
100 Days of Coding Challenge – Day 28 📌 Problem: Find First and Last Position of Element in Sorted Array 💻 Language: Java 🧠 Concept Used: Binary Search (Modified) 🔍 Platform: LeetCode Today’s challenge was to find the starting and ending positions of a target value in a sorted array. If the target is not found → return [-1, -1]. Approach: ✔ Use Binary Search since the array is sorted ✔ Perform two searches: • One to find the first occurrence • One to find the last occurrence ✔ Narrow the search space based on comparisons ✔ Combine both results into the final answer Time Complexity: O(log n) Space Complexity: O(1) 🔗 Problem Link: https://lnkd.in/gK5EM63Z 🔗 Code: https://lnkd.in/gMwMsF7R #100DaysOfCode #Day28 #Java #DSA #LeetCode #BinarySearch #Arrays #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 8 of #100DaysOfCode Solved LeetCode Problem 69 – Sqrt(x) today using a simple loop approach! 🔍 Problem Summary: Given a non-negative integer "x", return the square root of "x" rounded down to the nearest integer (without using built-in functions). 💡 What I practiced: - Basic iteration using loops - Handling overflow using "long" - Understanding how brute force works before optimizing ⚡ Approach (Brute Force): Start from "i = 0" and keep checking: "i * i <= x" Stop when it exceeds "x", and return "i - 1" 💻 Java Code: public static int mySqrt(int x) { long i = 0; while (i * i <= x) { i++; } return (int)(i - 1); } 📌 Takeaway: Starting with a simple loop helps build clarity. Optimization (like Binary Search) can come next! #LeetCode #Java #100DaysOfCode #CodingJourney #BasicsFirst #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 97 of My 100 Days LeetCode Challenge | Java Today’s problem was a solid exercise in matrix processing and prefix sum optimization. The goal was to count the number of submatrices whose sum is less than or equal to a given value (k). A brute-force approach would be too slow, so the key was to use 2D prefix sums to efficiently compute submatrix sums. By converting the matrix into a prefix sum matrix, we can calculate the sum of any submatrix in constant time, making the overall solution much more efficient. ✅ Problem Solved: Count Submatrices With Sum ≤ K ✔️ All test cases passed (859/859) ⏱️ Runtime: 7 ms 🧠 Approach: 2D Prefix Sum 🧩 Key Learnings: ● Prefix sums are powerful for optimizing repeated range sum queries. ● 2D prefix sums extend the same idea from arrays to matrices. ● Preprocessing can drastically reduce computation time. ● Avoiding brute force is key in large input problems. ● Matrix problems often become easier with the right transformation. This problem reinforced how preprocessing techniques like prefix sums can turn complex problems into efficient solutions. 🔥 Day 97 complete — sharpening matrix optimization and prefix sum skills. #LeetCode #100DaysOfCode #Java #PrefixSum #Matrix #Algorithms #ProblemSolving #DSA #CodingJourney #Consistency
To view or add a comment, sign in
-
-
Day 95 - LeetCode Journey Solved LeetCode 1700: Number of Students Unable to Eat Lunch in Java ✅ At first it looks like a simulation problem using queue + stack… but the real trick is to avoid simulation completely. Instead of rotating the queue again and again, I simply counted preferences and matched them with sandwiches. Smart counting beats brute force 💡 Key takeaways: • Avoid unnecessary simulation • Use counting instead of queue operations • Greedy thinking simplifies the problem • Writing clean and optimal logic ✅ All test cases passed ⚡ O(n) time and O(1) space Sometimes the best solution is not doing what the problem is asking literally 🔥 #LeetCode #DSA #Java #Greedy #ProblemSolving #CodingJourney #InterviewPrep #Consistency #100DaysOfCode
To view or add a comment, sign in
-
-
Day 55 of #100DaysOfLeetCode 💻✅ Solved #434. Number of Segments in a String problem in Java. Approach: • Initialized a counter to track number of words (segments) • Traversed the string character by character • Checked for non-space characters • If the current character is not a space and either it is the first character or the previous character is a space, incremented the count • This ensures counting only the starting of each word Performance: ✓ Runtime: 0 ms (Beats 100% submissions) 🚀 ✓ Memory: 41.83 MB (Beats 99.81% submissions) Key Learning: ✓ Practiced string traversal without using extra space ✓ Learned how to identify word boundaries efficiently ✓ Improved logic building for handling spaces and edge cases Learning one problem every single day 🚀 #Java #LeetCode #DSA #Strings #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
Day 96 - LeetCode Journey Solved LeetCode 901: Online Stock Span in Java ✅ This problem is all about recognizing the pattern and using the right data structure. Instead of checking previous prices one by one, I used a Monotonic Stack to efficiently calculate spans. Every element is pushed and popped at most once → super optimized 🔥 Key idea: Keep removing smaller or equal previous prices and accumulate their spans. Key takeaways: • Monotonic Stack concept (very important) • Avoiding nested loops using stack optimization • Efficient span calculation • Thinking in patterns, not brute force ✅ All test cases passed ⚡ O(n) time and O(n) space This is one of those problems that truly levels up your stack game 💯 #LeetCode #DSA #Java #Stack #MonotonicStack #ProblemSolving #CodingJourney #InterviewPrep #Consistency #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 2/75 – LeetCode Challenge Today I solved the problem “Greatest Common Divisor of Strings” 💡 In this problem, we are given two strings and need to find the largest string that can divide both of them (i.e., by repeating itself). 🔍 Key Learnings: Pattern recognition is very important The condition (str1 + str2 == str2 + str1) simplifies the problem Mathematical concepts like GCD can be applied to strings 🤯 🧠 Approach: First, check if both strings are compatible Then, find the GCD of their lengths Finally, take the substring of that length as the answer 💻 Language: Java Staying consistent and learning every day 🔥 #LeetCode #CodingJourney #Java #DSA #100DaysOfCode #LearningInPublic
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