📌 LeetCode Daily Challenge — Day 16 Problem: 1878. Biggest Three Rhombus Sums in a Grid Topic: Array, Matrix, Sorting, Simulation 📌 Quick Problem Sense: You're given an m × n integer grid. A rhombus is a square rotated 45°, you sum only its border cells, not the inside. Find the top 3 distinct rhombus sums across all valid sizes and positions in descending order. A rhombus of size 0 is just a single cell, every cell counts! 🧠 Approach (Simple Thinking): 🔹 Every cell (i, j) can be the center of a rhombus of size r = 0, 1, 2, ... 🔹 Size 0 = just the cell itself, add it directly 🔹 For size r ≥ 1, walk the 4 diagonal sides of the rhombus border 🔹 Start at the top tip (i-r, j) and walk with directions: (+1,+1), (+1,-1), (-1,-1), (-1,+1) 🔹 Each side has exactly r steps, skip the last cell to avoid double counting corners 🔹 Use a TreeSet capped at size 3 to track top 3 distinct sums automatically 🔹 TreeSet handles sorting + deduplication, just poll the top 3 at the end! ⏱️ Time Complexity: Iterating all centers and sizes → O(m × n × min(m,n)²) Manageable for given constraints! 📦 Space Complexity: TreeSet holds at most 3 values → O(1) No extra matrix or prefix array needed! I wrote a full breakdown with dry run, real-life analogy, and step-by-step code walkthrough here 👇 https://lnkd.in/ggewfAsx If you solved it using diagonal prefix sums or a different top-K trick, drop it in the comments, always curious to see how others think about it 💬 See you in the next problem 👋 #Java #ProblemSolving #DSA #CodingInterview
Biggest Rhombus Sums in Grid
More Relevant Posts
-
🚀 Day 74/100 – LeetCode Challenge 🔍 Problem Solved: Find the Duplicate Number (287) Today’s problem was a great reminder that sometimes the best solutions come from thinking differently 💡 Instead of using extra space or modifying the array, I used Floyd’s Cycle Detection Algorithm (Tortoise & Hare) — treating the array like a linked list to detect a cycle. 👉 Key Learnings: • Arrays can sometimes be visualized as linked structures • Cycle detection is not just for linked lists! • Optimizing for O(1) space is a common interview expectation ⚡ Approach: Use two pointers (slow & fast) First, find intersection point Then, find the cycle start → duplicate number ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) ✅ Successfully passed all test cases! Consistency > Perfection. Let’s keep going 💪 #Day74 #LeetCode #100DaysOfCode #Java #CodingInterview #ProblemSolving #DataStructures #Algorithms
To view or add a comment, sign in
-
-
Day 51/100 | #100DaysOfDSA 🚀⚡ Today’s problem: Product of Array Except Self Given an array, return a new array where each element is the product of all elements except itself — without using division. Brute force? Too slow. Division? Not allowed. Optimized Approach: • Build prefix product array (left to right) • Then multiply with suffix product (right to left) • No extra space needed (excluding output) Time Complexity: O(n) Space Complexity: O(1) Big takeaway: Breaking a problem into prefix + suffix patterns can unlock optimal solutions. Clean logic. Efficient approach. 💯 Day 51 done. 🔥 #100DaysOfCode #LeetCode #DSA #Algorithms #Arrays #PrefixSum #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
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 45 of #100DaysOfCode Solved 209. Minimum Size Subarray Sum on LeetCode 📊 🧠 Key Insight: We need to find the smallest length subarray whose sum is ≥ target. A brute-force approach would be O(n²), but this can be optimized using the Sliding Window (Two Pointers) technique. ⚙️ Approach: 1️⃣ Initialize two pointers: left = 0 and iterate right 2️⃣ Keep adding elements to curr_sum 3️⃣ Once curr_sum ≥ target, try to shrink the window: 🔹Update minimum length 🔹Remove nums[left] and move left forward 4️⃣ Repeat until the entire array is processed This ensures we always maintain the smallest valid window. ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #SlidingWindow #Arrays #Algorithms #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 76 — Slow & Fast Pointer (Find the Duplicate Number) Continuing the cycle detection pattern — today I applied slow‑fast pointers to an array problem where the values act as pointers to indices. 📌 Problem Solved: - LeetCode 287 – Find the Duplicate Number 🧠 Key Learnings: 1️⃣ The Problem Twist Given an array of length `n+1` containing integers from `1` to `n` (inclusive), with one duplicate. We must find the duplicate without modifying the array and using only O(1) extra space. 2️⃣ Why Slow‑Fast Pointer Works Here - Treat the array as a linked list where `i` points to `nums[i]`. - Because there’s a duplicate, two different indices point to the same value → a cycle exists in this implicit linked list. - The duplicate number is exactly the entry point of the cycle (same logic as LeetCode 142). 3️⃣ The Algorithm in Steps - Phase 1 (detect cycle): `slow = nums[slow]`, `fast = nums[nums[fast]]`. Wait for them to meet. - Phase 2 (find cycle start): Reset `slow = 0`, then move both one step at a time until they meet again. The meeting point is the duplicate. 4️⃣ Why Not Use Sorting or Hashing? - Sorting modifies the array (not allowed). - Hashing uses O(n) space (not allowed). - Slow‑fast pointer runs in O(n) time and O(1) space — perfect for the constraints. 💡 Takeaway: This problem beautifully demonstrates how the slow‑fast pattern transcends linked lists. Any structure where you can define a “next” function (here: `next(i) = nums[i]`) can be analyzed for cycles. Recognizing this abstraction is a superpower. No guilt about past breaks — just another pattern mastered, one day at a time. #DSA #SlowFastPointer #CycleDetection #FindDuplicateNumber #LeetCode #CodingJourney #Revision #Java #ProblemSolving #Consistency #GrowthMindset #TechCommunity #LearningInPublic
To view or add a comment, sign in
-
🧠 Day 183 — Reverse Submatrix 🔄📊 Today solved a clean and intuitive matrix manipulation problem: Reverse a Submatrix. 📌 Problem Goal Given a matrix and parameters (x, y, k): ✔️ Select a k × k submatrix starting from (x, y) ✔️ Reverse it vertically (top ↔ bottom rows) 🔹 Core Idea Instead of touching every element randomly, we: 👉 Focus only on the selected submatrix boundaries 👉 Swap rows from top to bottom This works like reversing an array, but applied to rows inside a matrix window. 🔹 Approach 1️⃣ Set two pointers: • One at the top row • One at the bottom row 2️⃣ Swap entire rows (column by column) within the submatrix 3️⃣ Move inward until all rows are reversed 🔹 Why It Works We are effectively reversing the order of rows in the submatrix while keeping column positions intact. This ensures: ✔️ Correct transformation ✔️ In-place modification ✔️ Efficient traversal 🧠 Key Learning ✔️ Matrix problems often reduce to controlled pointer movement ✔️ Think in terms of sub-boundaries instead of full matrix ✔️ Row/column swaps are powerful tools for transformations 🚀 Momentum Status: Strong consistency with arrays, matrices, and trees. On to Day 184. #DSA #Matrix #ProblemSolving #Java #CodingJourney #LeetCode #ConsistencyWins
To view or add a comment, sign in
-
-
Day 56/100 | #100DaysOfDSA 🧠⚡ Today’s problem: String Compression A clean in-place array manipulation problem. Core idea: Compress consecutive repeating characters and store the result in the same array. Approach: • Traverse the array using a pointer • Count consecutive occurrences of each character • Write the character to the array • If count > 1 → write its digits one by one • Move forward and repeat Key insight: We don’t need extra space — just carefully manage read & write pointers. Time Complexity: O(n) Space Complexity: O(1) Big takeaway: In-place algorithms require precise pointer control but give optimal space efficiency. Mastering these improves real-world memory optimization skills. 🔥 Day 56 done. #100DaysOfCode #LeetCode #DSA #Algorithms #Strings #TwoPointers #InPlace #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
Day 33/75 — Sort an Array (Merge Sort) Today’s problem required sorting an array in O(n log n) time without using built-in sort. Approach: • Divide array into two halves • Recursively sort both halves • Merge sorted halves Key idea: return merge(left, right); Merge step ensures elements are placed in sorted order. Time Complexity: O(n log n) Space Complexity: O(n) This problem reinforced the concept of Divide & Conquer. 33/75 🚀 #Day33 #DSA #MergeSort #Java #Algorithms #LeetCode
To view or add a comment, sign in
-
-
Day 6 of #100DaysOfLeetCode ✅ Today I solved LeetCode 128 — Longest Consecutive Sequence. 🧩 Problem Summary: Given an unsorted array of integers, the task is to find the length of the longest sequence of consecutive numbers. The challenge is to solve it in O(n) time complexity, which means sorting is not allowed. 💡 Key Learning: Instead of sorting, I used a HashSet for O(1) lookup. The main intuition: 👉 A number starts a sequence only if (num - 1) does NOT exist in the set. Then we expand forward (num + 1) to count the sequence length. ⚡ Concepts Practiced: • HashSet / Hashing • Optimized Searching (O(1) lookup) • Sequence Detection Pattern • Time Complexity Optimization 📈 Time Complexity: O(n) 📦 Space Complexity: O(n) Every day improving problem-solving skills and understanding data structures deeper 🚀 #DSA #LeetCode #Java #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 #100DaysOfCode | Day 48 🔍 Solved: Find Peak Element Today I worked on an interesting Binary Search problem where the goal was to find a peak element in an array. 💡Key Insight: By comparing the middle element with its next element, we can determine the direction of the peak. 📌 Approach: ✔ Applied Binary Search for O(log n) efficiency ✔ Compared mid with next element ✔ Moved towards the increasing side to find peak ✔ Reduced search space step by step Why this works: Since adjacent elements are not equal, there will always be at least one peak. By comparing neighboring elements, we can decide the direction and reduce the search space efficiently. 🎯 What I Learned: This problem showed how Binary Search can be used beyond searching—especially for identifying patterns like peaks in arrays. #Java #DSA #LeetCode #BinarySearch #CodingJourney #ProblemSolving #TechSkills 🚀
To view or add a comment, sign in
-
Explore related topics
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
Keep it up