Melt (Unpivot)

MediumData EngineeringSortingMatrixSQL

Description

Given a 2D table, unpivot it from wide to long format: return one [rowIndex, columnIndex, value] triple per cell, in row-major order.

Examples

Input:[[5,6],[7,8]]
Output:[[0,0,5],[0,1,6],[1,0,7],[1,1,8]]
Explanation:

Every cell of the table becomes its own row labelled with its row and column position alongside the value.

Input:[[1]]
Output:[[0,0,1]]
Explanation:

Every cell of the table becomes its own row labelled with its row and column position alongside the value.

Input:[[1,2,3]]
Output:[[0,0,1],[0,1,2],[0,2,3]]
Explanation:

Every cell of the table becomes its own row labelled with its row and column position alongside the value.

Constraints

  • 1 ≤ rows, cols ≤ 10³

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.