🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/gWPpdhN8 💡 My thought process: The algorithm goes through each cell in the grid and treats every cell as the top vertex of a rhombus. For every possible size of the rhombus, it calculates the positions of the left, right, and bottom vertices. Before adding up the values, it checks if these vertices stay within the grid boundaries. If any vertex is outside the grid, it’s not possible to create larger rhombuses from that position, so the loop stops. The calculate function finds the border sum of the rhombus. It starts at the top vertex and moves along the four edges: from top to left, left to bottom, bottom to right, and back to top. The traversal moves diagonally across the grid and continues until it reaches the exact target vertex. The condition i != target_row || j != target_col makes sure the loop keeps going until both coordinates match the goal. For each rhombus sum found, the algorithm updates three variables: sum1, sum2, and sum3. These hold the largest, second-largest, and third-largest distinct sums. It skips duplicate sums to store only unique values. The algorithm also handles single-cell rhombuses separately since they represent rhombuses of size zero. Finally, the function returns the largest distinct sum it found. If three exist, it returns all three. If there are fewer distinct sums, it returns only the ones available, listed in descending order. 👉 My Solution: https://lnkd.in/gYi2CDgR If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
LeetCode Challenge: Rhombus Sums in Grid
More Relevant Posts
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/gbtaBXYW 💡 My thought process: The algorithm first calculates a normalized shift value using k % n. Shifting a row by its total length, n, returns the original configuration. Therefore, any shift k is the same as k modulo n. Instead of rotating memory physically or creating a temporary matrix, the code uses a virtual mapping strategy. It goes through each cell (i, j) and finds the index of the element that would be in that position after the shift. For even rows (left shift), to find the original value that moves into position j after a left shift, the code looks at the index (j - shift). It manages negative results by wrapping around with an addition of n. For odd rows (right shift), it checks the index (j + shift) % n. The function has a fail-fast mechanism; it returns false right away if it finds the first difference between a cell and its shifted counterpart. 👉 My Solution: https://lnkd.in/g5myRYRj If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/gSiw_GQq 💡 My thought process: It uses a two-pointer approach. One pointer starts at the top row of the submatrix, and the other starts at the bottom row. For each pair of rows, it goes through all columns within the submatrix range and swaps the elements column by column. This process continues until the top and bottom pointers meet or cross. This ensures the submatrix is reversed in place without using any extra space. The updated grid is then returned. 👉 My Solution: https://lnkd.in/g_cAfrtb If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/gK993p-S 💡 My thought process: The main function, minAbsDiff, goes through all possible top-left positions of the k × k submatrices. For each position (i, j), it calls the helper function solve to find the result for that submatrix and stores it in the answer matrix, which has a size of (m − k + 1) × (n − k + 1). The solve function collects all elements from the k × k submatrix starting at (i, j) and puts them into a set. This automatically sorts the elements and removes duplicates. If the set has only one unique element, the function returns 0 because all values are the same. If there are multiple unique elements, the function goes through the sorted set and calculates the absolute difference between each pair of consecutive elements. Since the set is sorted, the minimum absolute difference will be between adjacent elements. The smallest difference is tracked and returned. The overall time complexity is O((m − k + 1) × (n − k + 1) × k² log(k²)) because each submatrix requires inserting k² elements into a set and iterating through it. The space complexity is O(k²) for storing elements of each submatrix. 👉 My Solution: https://lnkd.in/gj6juuUU If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
𝐆𝐅𝐆 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝: 𝐈𝐧𝐭𝐞𝐫𝐬𝐞𝐜𝐭𝐢𝐨𝐧 𝐨𝐟 𝐓𝐰𝐨 𝐋𝐢𝐧𝐤𝐞𝐝 𝐋𝐢𝐬𝐭𝐬 Solved another important Linked List problem that focuses on hashing + pointer manipulation 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐭𝐚𝐭𝐞𝐦𝐞𝐧𝐭: Given two linked lists, return a new list that contains the intersection of elements present in both lists (including duplicates). 💡 𝐊𝐞𝐲 𝐈𝐧𝐬𝐢𝐠𝐡𝐭: To efficiently find common elements, we can use a hash map to store frequencies of elements from one list and then traverse the other list to build the intersection. ⚙️ 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 𝐈 𝐔𝐬𝐞𝐝: 1. Store Elements of Second List: • Traverse head2 • Store frequency of each value in an unordered_map 2. Traverse First List: • For each node in head1, check if it exists in the map 3. Build Intersection List: a. If element exists: • Add it to the result list • Decrease its frequency in the map (to handle duplicates correctly) 4. Maintain Pointers: • IL → head of intersection list • it → pointer to build the result list 5. Terminate List Properly: • Ensure last node points to NULL to avoid unwanted links 🧠 𝐖𝐡𝐲 𝐓𝐡𝐢𝐬 𝐖𝐨𝐫𝐤𝐬: The hash map allows O(1) lookup for elements of the second list. By reducing frequency after each match, we correctly handle duplicate values. This avoids nested loops and reduces complexity significantly. 🧾𝐂𝐨𝐫𝐞 𝐋𝐨𝐠𝐢𝐜: if(map[curr->data] > 0){ it->next = curr; it = it->next; map[curr->data]--; } 📈𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: Time Complexity: O(n + m) Space Complexity: O(m) (for hash map) ✨ 𝐖𝐡𝐚𝐭 𝐈 𝐋𝐞𝐚𝐫𝐧𝐞𝐝: Hashing is powerful for intersection-type problems Handling duplicates carefully is crucial Linked List problems often require combining multiple concepts 🔥 Improving DSA skills step by step! #DSA #LinkedList #GeeksforGeeks #Coding #CPP #ProblemSolving #Algorithms
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/gVK8xX3p 💡 My thought process: The solution uses a top-down dynamic programming approach with memoization. Each cell stores a pair of values: the maximum product and the minimum product achievable from that cell to the destination. The minimum value is necessary because multiplying two negative numbers can lead to a larger positive result later. The recursive function explores both possible moves—right and down—from the current cell and retrieves their stored results. For each direction, it considers both the maximum and minimum products returned and multiplies them by the current cell’s value. All combinations are evaluated, and the overall maximum and minimum are selected and stored in the DP table for that cell. Memoization ensures that each cell is computed only once, reducing the time complexity to O(m × n). The base case occurs at the bottom-right cell, where both maximum and minimum values equal the cell’s value. Invalid positions return sentinel values to indicate they should not be considered. Finally, the function checks the result from the starting cell. If the maximum product is negative, it returns -1. Otherwise, it returns the result modulo 1e9 + 7. 👉 My Solution: https://lnkd.in/gcKfTVNq If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
🚀𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝: 𝐌𝐞𝐫𝐠𝐞 𝐍𝐨𝐝𝐞𝐬 𝐢𝐧 𝐁𝐞𝐭𝐰𝐞𝐞𝐧 𝐙𝐞𝐫𝐨𝐬 Solved a very interesting Linked List problem that focuses on pointer manipulation and in-place modification 🔍 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐭𝐚𝐭𝐞𝐦𝐞𝐧𝐭: Given a linked list where: • The list starts and ends with 0 • Between every pair of 0s, there are some positive integers 👉 Merge all nodes between two 0s into a single node whose value is the sum of those nodes. 💡𝐊𝐞𝐲 𝐈𝐧𝐬𝐢𝐠𝐡𝐭: Instead of creating a new list, we can: 👉 Use the existing nodes and modify them in-place This makes the solution more efficient in terms of space. ⚙️ 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 𝐈 𝐔𝐬𝐞𝐝: 1. Use Two Pointers: • slow → points to position where result will be stored • fast → traverses the list 2. Traverse the List: • Keep adding values to sum until we hit a 0 3. When Encountering 0: • Assign sum to slow->val • Move slow forward • Reset sum to 0 4. Track Last Valid Node: • Keep pointer newLastNode to end the final list properly 5. Terminate the List: • Set newLastNode->next = NULL 🧠 𝐖𝐡𝐲 𝐓𝐡𝐢𝐬 𝐖𝐨𝐫𝐤𝐬: We reuse the original list nodes instead of allocating new memory. Each segment between zeros is compressed into a single node efficiently. 🧾 𝐂𝐨𝐫𝐞 𝐋𝐨𝐠𝐢𝐜: if(fast->val != 0){ sum += fast->val; } else{ slow->val = sum; slow = slow->next; sum = 0; } 📈𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: • Time Complexity: O(n) • Space Complexity: O(1) ✨ 𝐖𝐡𝐚𝐭 𝐈 𝐋𝐞𝐚𝐫𝐧𝐞𝐝: • In-place modification can optimize space • Two-pointer technique is very powerful • Always track the end of the new list carefully 🔥 Practicing more Linked List problems to strengthen fundamentals! #LeetCode #DSA #LinkedList #Coding #CPP #ProblemSolving #Algorithms
To view or add a comment, sign in
-
-
🚀 𝐆𝐅𝐆 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝: 𝐃𝐞𝐥𝐞𝐭𝐞 𝐍 𝐍𝐨𝐝𝐞𝐬 𝐀𝐟𝐭𝐞𝐫 𝐌 𝐍𝐨𝐝𝐞𝐬 (𝐋𝐢𝐧𝐤𝐞𝐝 𝐋𝐢𝐬𝐭) Solved another interesting Linked List problem that tests pointer manipulation and recursion 💡 🔍 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐭𝐚𝐭𝐞𝐦𝐞𝐧𝐭: Given a linked list, delete N nodes after every M nodes until the end of the list. 💡𝐊𝐞𝐲 𝐈𝐧𝐬𝐢𝐠𝐡𝐭: The problem follows a repeating pattern: 👉 Keep M nodes → Delete N nodes → Repeat Handling this efficiently requires careful pointer movement and linking. ⚙️ 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 𝐈 𝐔𝐬𝐞𝐝: 1. Edge Case Handling: • If the list is empty → return head • If m == 0 → delete entire list 2. Traverse M nodes: • Move pointer to the M-th node • This part of the list remains intact 3. Delete next N nodes: • Start from (M+1)-th node • Delete nodes one by one using a loop 4. Recursive Connection: • After deletion, recursively call function for the remaining list • Link the M-th node to the result of recursion 🧠 𝐖𝐡𝐲 𝐓𝐡𝐢𝐬 𝐖𝐨𝐫𝐤𝐬: The recursion ensures that the same pattern (skip M, delete N) is applied to the entire list without writing repetitive code. It simplifies the logic by breaking the problem into smaller subproblems. 🧾 𝐂𝐨𝐫𝐞 𝐋𝐨𝐠𝐢𝐜: MthNode->next = linkdelete(it, n, m); 📈 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: • Time Complexity: O(n) (each node visited once) • Space Complexity: O(n/m) (due to recursion stack) ✨𝐖𝐡𝐚𝐭 𝐈 𝐋𝐞𝐚𝐫𝐧𝐞𝐝: • Linked Lists require precise pointer handling • Recursion can simplify repetitive patterns • Always handle edge cases like m = 0 carefully 🔥 Practicing more Linked List problems to strengthen fundamentals! . . #DSA #LinkedList #GeeksforGeeks #Coding #CPP #ProblemSolving #Algorithms
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/giAcdv2U 💡 My thought process: This solution uses 3D dynamic programming to find the maximum coins that can be collected from the top-left to the bottom-right of a grid, allowing up to 2 neutralizations for negative cells. The state dp[i][j][k] stores the maximum coins reachable at cell (i, j) using k neutralizations. For each cell, transitions are taken from the left and top cells. If the current cell is negative, two options are considered: either neutralize it (if k > 0) and carry forward the previous value without adding the negative, or do not neutralize and add the cell value. For non-negative cells, the value is simply added to the best of the previous states. The starting cell is initialized separately based on whether it is neutralized or not. The final answer is the maximum value among dp[m-1][n-1][0], dp[m-1][n-1][1], and dp[m-1][n-1][2]. 👉 My Solution: https://lnkd.in/gZWS3Kns If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
Upsolved an interesting problem from a recent LeetCode contest: 3628. Maximum Number of Subsequences After One Insertion At first glance, it looks simple — but the real challenge is in structuring the thinking correctly. 🧠 Key Idea We are allowed to insert at most one character, so the optimal solution must come from evaluating all possible insertion scenarios: - Insert "L" - Insert "C" - Insert "T" Instead of brute force, we break the problem into counting subsequences efficiently. 🔍 Observations A valid subsequence follows: L → C → T So we reverse-engineer what each insertion needs: 1️⃣ Insert "L" To maximize impact, "L" should contribute to as many "CT" pairs as possible. 👉 Compute from right to left: - Count of "T" - Then count of "CT" So: New subsequences = existing LCT + total CT 2️⃣ Insert "T" Now we want maximum "LC" pairs. 👉 Compute from left to right: - Count of "L" - Then count of "LC" So: New subsequences = existing LCT + total LC 3️⃣ Insert "C" This is the most interesting case. We insert "C" at every possible position (n+1 positions), and for each position: - Count of "L" before it - Count of "T" after it 👉 Contribution: L_before × T_after We take the maximum over all positions. ⚙️ Final Strategy - Precompute: - Prefix count of "L" - Suffix count of "T" - Track: - "LC" and "CT" counts - Existing "LCT" count - Evaluate all 3 insertion options ⏱️ Complexity - Time: O(n) - Space: O(n) (can be optimized further) Also, a subtle bug I hit initially: 👉 For "C" insertion, you must consider all n+1 positions, not just internal indices. How can the Space Complexity be optimized further? Please let me know in the comments, I'm all ears. #LeetCode #DSA #Algorithms #CodingInterview #SoftwareEngineering
To view or add a comment, sign in
-
🚀 Day 11 of 100 Days LeetCode Challenge Problem: Complement of Base 10 Integer Today’s problem is a clean example of bit manipulation fundamentals 🔥 💡 Key Insight: To find the complement: Convert the number to binary Flip all bits (0 → 1, 1 → 0) Convert it back to decimal 👉 But here’s the trick: We only flip significant bits (ignore leading zeros) 🔍 Optimized Approach: Create a bitmask with all bits set to 1 (same length as n) Then: complement = n XOR mask 👉 Example: n = 5 → (101) mask = 111 Result = 101 ⊕ 111 = 010 → 2 🔥 What I Learned Today: XOR is powerful for bit flipping operations Bitmasking simplifies binary problems Always consider binary representation constraints 📈 Challenge Progress: Day 11/100 ✅ Bit by bit improving! LeetCode, Bit Manipulation, XOR, Binary Representation, Bitmasking, DSA Practice, Coding Challenge, Problem Solving, Algorithms #100DaysOfCode #LeetCode #DSA #CodingChallenge #BitManipulation #Binary #XOR #ProblemSolving #TechJourney #ProgrammerLife #SoftwareDeveloper #CodingLife #LearnToCode #Developers #Consistency #GrowthMindset #InterviewPrep
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