O(N^3) solution for 50x50 matrix

In this problem we need to not only traverse the matrix, but also each diagonal, bringing the total complexity to N^3 or 125K iterations... code is below, cheers, ACC

Difference of Number of Distinct Values on Diagonals - LeetCode

2711. Difference of Number of Distinct Values on Diagonals
Medium

Given a 0-indexed 2D grid of size m x n, you should find the matrix answer of size m x n.

The value of each cell (r, c) of the matrix answer 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 matrix grid.
  • Let bottomRight[r][c] be the number of distinct values in the bottom-right diagonal of the cell (r, c) in the matrix grid.

Then answer[r][c] = |topLeft[r][c] - bottomRight[r][c]|.

Return the matrix answer.

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 and r1 < r. Similarly is defined bottom-right diagonal.

 

Example 1:


Input: grid = [[1,2,3],[3,1,5],[3,2,1]]
Output: [[1,1,0],[1,0,1],[0,1,1]]
Explanation: The 1st diagram denotes the initial grid. 
The 2nd diagram denotes a grid for cell (0,0), where blue-colored cells are cells on its bottom-right diagonal.
The 3rd diagram denotes a grid for cell (1,2), where red-colored cells are cells on its top-left diagonal.
The 4th diagram denotes a grid for cell (1,1), where blue-colored cells are cells on its bottom-right diagonal and red-colored cells are cells on its top-left diagonal.
- The cell (0,0) contains [1,1] on its bottom-right diagonal and [] on its top-left diagonal. The answer is |1 - 0| = 1.
- The cell (1,2) contains [] on its bottom-right diagonal and [2] on its top-left diagonal. The answer is |0 - 1| = 1.
- The cell (1,1) contains [1] on its bottom-right diagonal and [1] on its top-left diagonal. The answer is |1 - 1| = 0.
The answers of other cells are similarly calculated.

Example 2:

Input: grid = [[1]]
Output: [[0]]
Explanation: - The cell (0,0) contains [] on its bottom-right diagonal and [] on its top-left diagonal. The answer is |0 - 0| = 0.

 

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n, grid[i][j] <= 50

public int[][] DifferenceOfDistinctValues(int[][] grid)
{
    int[][] retVal = new int[grid.Length][];
    for (int r = 0; r < grid.Length; r++) retVal[r] = new int[grid[r].Length];

    for (int r = 0; r < grid.Length; r++)
    {
        for (int c = 0; c < grid[r].Length; c++)
        {
            Hashtable htLeft = new Hashtable();
            int lr = r - 1;
            int lc = c - 1;
            while (lr >= 0 && lc >= 0)
            {
                if (!htLeft.ContainsKey(grid[lr][lc])) htLeft.Add(grid[lr][lc], true);
                lr--;
                lc--;
            }

            Hashtable htRight = new Hashtable();
            int rr = r + 1;
            int rc = c + 1;
            while (rr < grid.Length && rc < grid[rr].Length)
            {
                if (!htRight.ContainsKey(grid[rr][rc])) htRight.Add(grid[rr][rc], true);
                rr++;
                rc++;
            }

            retVal[r][c] = Math.Abs(htLeft.Count - htRight.Count);
        }
    }

    return retVal;
}

Comments

Popular posts from this blog

Changing the root of a binary tree

ProjectEuler Problem 719 (some hints, but no spoilers)

Hard Leetcode Problem: Grid Illumination