LeetCode Challenge: Rhombus Sums in Grid

🚀 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

  • text

To view or add a comment, sign in

Explore content categories