2711. Difference of Number of Distinct Values on Diagonals
Given a 0-indexed 2D
grid
of sizem x n
, you should find the matrixanswer
of sizem x n
.The value of each cell
(r, c)
of the matrixanswer
is calculated in the following way:
- Let
topLeft[r][c]
be the number of distinct values in the top-left diagonal of the cell(r, c)
in the matrixgrid
.- Let
bottomRight[r][c]
be the number of distinct values in the bottom-right diagonal of the cell(r, c)
in the matrixgrid
.Then
answer[r][c] = |topLeft[r][c] - bottomRight[r][c]|
.Return the matrix
answer
.A matrix diagonal is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix’s end.
A cell
(r1, c1)
belongs to the top-left diagonal of the cell(r, c)
, if both belong to the same diagonal andr1 < r
. Similarly is defined bottom-right diagonal.
1 | class Solution { |