Problem · Matrix
Bounding Diagonal Weights
To veiw the original problem statement, pls check the source image below 🐘
Imagine you have a square grid of numbers, and this grid is n x n in size. Now, let's talk about something called a "bouncing diagonal." Here's how it works:
🌷༊·˚Credit to ꒰აCharlotte໒꒱ 🧡
Examples
01 · Example 1
matrix = [[2, 3, 2], [0, 2, 5], [1, 0, 1]] return = [1, 2, 0]
When traversing:
- Starting from matrix[0][0] (which is 2), the diagonal sum is 2 + 1 + 1 = 5. So, you create a pair: Pair(5, 2).
- Starting from matrix[1][0] (which is 0), the diagonal sum is 0 + 3 + 2 = 5. You create another pair: Pair (5, 0).
- Starting from matrix[2][0] (which is 1), the diagonal sum is 1 + 0 + 2 = 3. You create another pair: Pair (3, 1).
So your list pathSums would look like:
pathSums = [Pair(5, 2), Pair(5, 0), Pair (3, 1)]
After sorting pathSum by pathSum:
pathSums = [Pair(3, 1), Pair(5, 2), Pair(5, 0)]
The result would give you the values in the leftmost column [1, 2, 0] in order of increasing diagonal.
02 · Example 2
matrix = [[1, 3, 2, 5], [3, 2, 5, 0], [9, 0, 1, 3], [6, 1, 0, 8]] return = [1, 3, 9, 6]
🐳 Wasn't able to get access to the video explanation, following explanation is an educated guess. If you find anything wrong, feel free to lmk! Many thanks in advance!
For the given matrix, the weights of the cells in the leftmost column are calculated as follows:
- The weight of the first cell is the sum of the elements in its bouncing diagonal: 1 + 2 + 5 + 0 = 8.
- The weight of the second cell is the sum of the elements in its bouncing diagonal: 3 + 0 + 0 + 1 = 4.
- The weight of the third cell is the sum of the elements in its bouncing diagonal: 9 + 3 + 2 + 1 = 15.
- The weight of the fourth cell is the sum of the elements in its bouncing diagonal: 1 + 0 + 5 + 0 = 6.
The weights are [8, 4, 15, 6], but since we need to sort them by their values in descending order, the final output is [1, 3, 9, 6].
Constraints
🐾More Databricks problems
- Fastest SF CommutePHONE SCREEN · Seen May 2026
- Find First Anagram IndexPHONE SCREEN · Seen Apr 2026
- Minimize CommuteOA · Seen Apr 2026
- Difference Between Sums of PositionsSeen Sep 2024
- Longest Common Prefix of Number PairsSeen Sep 2024
- Subarray CountingSeen Sep 2024
- Write 'L' on MatrixSeen Sep 2024
- Binary String RequestsSeen Aug 2024
public int[] Diagonalweights(int[][] matrix) {
// write your code here
}
matrix[[2, 3, 2], [0, 2, 5], [1, 0, 1]]
expected[1, 2, 0]
sign in to submit