Problem · Matrix

Sorted Extended Matrix Diagonals

Learn this problem
MediumRoblox logoRobloxFULLTIMEOA
See Roblox hiring insights

Problem statement

Given a square matrix of characters matrix with a size of n x n, your task is to create a sorted list of matrix extended diagonals, where each diagonal has a length of n.

A matrix of size n x n has 2n - 1 diagonals parallel to the main diagonal, with each diagonal starting at its upper point and ending at its lower point. Since these diagonals have different lengths, traverse each one cyclically (go back to the starting point after reaching the end point) until it consists of n characters.

Sort the resulting strings of n characters in alphabetical order, and return an array of 2n - 1 integers, representing the diagonals' 1-based indices in their sorted order. In the case of alphabetically equal strings, their indices should be kept in the original order.

Below you can find an example of diagonals' numbering for a 5 x 5 matrix, where the number in the matrix corresponds to the diagonal index:

5 6 7 8 9
4 5 6 7 8
3 4 5 6 7
2 3 4 5 6
1 2 3 4 5

Note: You are not expected to provide the most optimal solution, but a solution with time complexity not worse than O(n^4) will fit within the execution time limit.

Function

solution(matrix: String[][]) → int[]

Examples

Example 1

matrix = [["b","b"],["c","a"]]return = [2,3,1]

For matrix = [["b", "b"], ["c", "a"]], the output should be solution(matrix) = [2, 3, 1].

More Roblox problems

drafts saved locally
public int[] solution(String[][] matrix) {
  // Write your code here.
}
matrix[["b","b"],["c","a"]]
expected[2,3,1]
checking account