Day 30/30 – LeetCode streak Problem: Get Biggest Three Rhombus Sums in a Grid You need the three largest distinct border sums of all rhombuses in the grid, in descending order. Core idea * A rhombus is defined by a center '(i, j)' and a radius or edge length 'k'. * If 'k = 0', the rhombus is just the single cell itself. * For 'k ≥ 1', the rhombus must stay within the grid so that all four corners exist. Approach * For every cell in the grid, treat it as the top vertex of a rhombus, and for each possible edge length k such that all four corners stay in bounds, walk its border and accumulate the sum. * Compute the maximum possible radius that still keeps the rhombus inside the grid. * For each valid radius, walk along the four edges of the rhombus border and accumulate the sum of those cells. * Because the grid size is small (at most 50 × 50), directly enumerating all rhombuses is efficient enough. Tracking the top three * Maintain a sorted set that keeps only distinct sums. * Every time a rhombus sum is computed, insert it into the set. * If the set size becomes greater than three, remove the smallest value. * At the end, the set contains the three largest distinct sums, which can be returned in descending order. Day 30 takeaway: This problem shows that sometimes a direct enumeration works perfectly when constraints are small. Instead of over-optimizing, you can simply generate all valid shapes and maintain the best results with a small ordered set. #leetcode #dsa #java #matrix #consistency
LeetCode Day 30: Biggest Three Rhombus Sums in a Grid
More Relevant Posts
-
Tried solving it with pure backtracking… but couldn’t optimize it efficiently without bitmasking. Here’s the approach that finally worked LeetCode 698 – Partition to K Equal Sum Subsets 🧠 Approach (Backtracking + Bitmasking + Memoization) We need to divide the array into k subsets such that each subset has equal sum. 🔹 Step 1: Feasibility Check Compute total sum If sum % k != 0 → return false 🔹 Step 2: Fix Target Each subset must have sum = target = sum / k 🔹 Step 3: Use Bitmask for State Represent picked elements using a bitmask mask helps track which elements are already used 🔹 Step 4: Build Subsets Recursively Keep adding unused elements to currSum If currSum == target → one subset is formed → Move to next subset (k - 1) and reset sum 🔹 Step 5: Memoization (Game Changer 🚀) Store results using mask If a configuration already failed → skip it ⚡ Key Insight Instead of managing k subsets explicitly: ->Focus on filling one subset at a time ->Reduce the problem (k) step by step ⏱️ Time Complexity O(N * 2^N) in worst case 2^N states from bitmask For each state, up to N choices ✔️ Memoization helps prune repeated states heavily 💬 Takeaway: Pure backtracking hits performance limits here — combining it with bitmasking + memoization is what makes it pass efficiently. #LeetCode #Backtracking #Bitmasking #Memoization #DSA #Java #CodingInterview
To view or add a comment, sign in
-
-
Day 15 of my #30DayCodeChallenge: Swapping Nodes in Pairs! The Problem: Swap Nodes in Pairs. Given a linked list, the goal is to swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (only nodes themselves may be changed). The Logic: This problem is a classic exercise in Pointer Manipulation and maintaining structural integrity in a Linked List: 1. Dummy Node Strategy: I initialized a dummy node pointing to the head. This acts as a fixed anchor, ensuring I can easily return the new head of the list even after the original head has been swapped. 2. The Three-Pointer Dance: To swap two nodes (cur and t), I need ✓ nage three specific connections for every pair: **-Point the previous node (pre) to the second node of the pair (t). **- Point the first node (cur) to the node following the pair (t. next). **- Point the second node (t) back to the first node (cur). 3. Iterative Traversal: The loop continues as long as there is a full pair remaining (cur ! = nul1 && cur.next ! = null). After each swap, the pre and cur pointers shift forward to prepare for the next pair. Another step closer to mastery. Onward to Day 16! #Java #Algorithms #DataStructures #LinkedList #ProblemSolving #150DaysOfCode #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 Solved: Find Dominant Index (LeetCode) Just solved an interesting problem where the goal is to find whether the largest element in the array is at least twice as large as every other number. 💡 Approach: 1. First, traverse the array to find the maximum element and its index. 2. Then, iterate again to check if the max element is at least twice every other element. 3. If the condition fails for any element → return "-1". 4. Otherwise → return the index of the max element. 🧠 Key Insight: Instead of comparing all pairs, just track the maximum and validate it — keeps the solution clean and efficient. ⚡ Time Complexity: O(n) ⚡ Space Complexity: O(1) 💻 Code (Java): class Solution { public int dominantIndex(int[] nums) { int max = -1; int index = -1; // Step 1: find max and index for (int i = 0; i < nums.length; i++) { if (nums[i] > max) { max = nums[i]; index = i; } } // Step 2: check condition for (int i = 0; i < nums.length; i++) { if (i == index) continue; if (max < 2 * nums[i]) { return -1; } } return index; } } 🔥 Got 100% runtime and 99%+ memory efficiency! #LeetCode #DSA #Java #Coding #ProblemSolving #Algorithms
To view or add a comment, sign in
-
-
🔥 Day 59/100 – LeetCode Challenge 📌 Problem Solved: Remove Duplicates from Sorted Array II (Medium) Today’s problem was a great exercise in in-place array manipulation and two-pointer technique. 💡 Key Idea: Since the array is already sorted, duplicates are adjacent. Instead of removing all duplicates, we allow each element to appear at most twice. 👉 The trick is to compare the current element with the element at index k-2. If they are the same → skip ❌ If different → keep it ✅ ⚙️ Approach: Initialize pointer k = 2 Traverse from index 2 Copy valid elements forward Maintain order without extra space 🧠 What I learned: How to efficiently handle constraints like “at most twice” Importance of thinking in terms of index relationships (k-2) Writing optimal O(n) solutions with O(1) space 📊 Performance: ⚡ Runtime: 0 ms (100%) 💾 Memory: 48.46 MB 💻 Tech Used: Java Consistency is key 🔑 — 59 days done, 41 more to go! #100DaysOfCode #LeetCode #Java #DataStructures #CodingChallenge #ProblemSolving #TechJourney
To view or add a comment, sign in
-
-
🚀 Day 30of LeetCode Odd Even Linked List — Clean Pointer Manipulation! Just solved a really interesting linked list problem that tests how well you understand pointer manipulation 👇 🔹 Problem: Rearrange a linked list such that all nodes at odd indices come first, followed by nodes at even indices — while maintaining their relative order. 👉 Example: Input: 1 → 2 → 3 → 4 → 5 Output: 1 → 3 → 5 → 2 → 4 💡 Key Insight: Instead of using extra space, we can split the list into: an odd-index list an even-index list Then simply connect them at the end! 🧠 Approach: Maintain two pointers: odd and even Keep track of evenHead Rearrange links in one traversal Attach even list after odd list ⚡ Complexity: Time: O(n) Space: O(1) (in-place) 🎯 What I learned: Mastering linked lists is all about pointer control Problems that look tricky often have simple in-place solutions Always think in terms of structure, not values #LeetCode #DataStructures #LinkedList #Java #CodingInterview #100DaysOfCode #ProblemSolving #TechJourney
To view or add a comment, sign in
-
-
🚀 Day 54 of #100DaysOfCode 🌱 Topic: Stack / Two Pointers ✅ Problem Solved: LeetCode 42 – Trapping Rain Water 🛠 Approach: Used a Monotonic Stack to calculate trapped water. Traversed the array and maintained a stack of indices. When current height is greater than stack top: Popped the top → this forms a “valley”. If stack becomes empty → break (no left boundary). Calculated: Height = min(left boundary, right boundary) − current height Width = distance between boundaries Added height × width to total water. Pushed current index into stack. This effectively finds water trapped between bars. #100DaysOfCode #Day54 #DSA #Stack #TwoPointers #LeetCode #HardProblems #Java #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
Day 72/100 Completed ✅ 🚀 Solved LeetCode – Matrix Diagonal Sum (Java) ⚡ Implemented an efficient approach to calculate the sum of both primary and secondary diagonals of a square matrix in a single traversal. Avoided double-counting the center element by handling the odd-sized matrix case separately. 🧠 Key Learnings: • Efficient diagonal traversal in a matrix • Handling overlapping elements (center in odd n) • Writing optimal O(n) solutions instead of nested loops • Clean and concise index-based logic 💯 This problem improved my understanding of matrix patterns and how to optimize traversal by reducing unnecessary iterations. 🔗 Profile: https://lnkd.in/gaJmKdrA #leetcode #datastructures #algorithms #java #matrix #arrays #problemSolving #100DaysOfCode 🚀
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 28 Today’s focus: Binary Search for minimum in rotated arrays. Problem solved: • Find Minimum in Rotated Sorted Array (LeetCode 153) Concepts used: • Binary Search • Identifying unsorted half • Search space reduction Key takeaway: The goal is to find the minimum element in a rotated sorted array. Using binary search, we compare the mid element with the rightmost element: • If nums[mid] > nums[right] → minimum lies in the right half • Else → minimum lies in the left half (including mid) This works because the rotation creates one unsorted region, and the minimum always lies in that region. By narrowing the search space each time, we achieve O(log n) time complexity. This problem highlights how slight modifications in array structure still allow binary search to work efficiently with the right observations. Continuing to strengthen binary search patterns and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 65 of #100DaysOfCode Solved 108. Convert Sorted Array to Binary Search Tree on LeetCode 🔗 🧠 Key Insight: To build a height-balanced BST from a sorted array: 👉 Always pick the middle element as root This ensures: Left half → left subtree Right half → right subtree Balanced structure 🔥 ⚙️ Approach (Divide & Conquer): 1️⃣ Choose middle index: 🔹 mid = start + (end - start) / 2 2️⃣ Create root node: 🔹 node = new TreeNode(nums[mid]) 3️⃣ Build left subtree: 🔹 node.left = build(start, mid - 1) 4️⃣ Build right subtree: 🔹 node.right = build(mid + 1, end) 5️⃣ Return root ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(log n) (recursion stack) #100DaysOfCode #LeetCode #DSA #BinaryTree #BST #DivideAndConquer #Recursion #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
💡 Day 36 of LeetCode Problem Solved! 🔧 🌟11. Container With Most Water🌟 Task : You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]). Find two lines that together with the x-axis form a container, such that the container contains the most water. Return the maximum amount of water a container can store. Notice that you may not slant the container. Example 1: GInput: height = [1,8,6,2,5,4,8,3,7] Output: 49 Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49. Example 2: Input: height = [1,1] Output: 1 #LeetCode #Java #DSA #ProblemSolving #Consistency #100DaysOfChallenge #CodingJourney #KeepGrowing
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