Special Positions in a Binary Matrix Lab

Easy
1
44.4% Acceptance

In this lab, you will work on finding the special positions in a m x n binary matrix. A position (i, j) is called special if mat[i][j] == 1 and all other elements in row i and column j are 0 (rows and columns are 0-indexed). You will be required to implement a function numSpecial(mat) that takes in a 2D binary matrix mat and returns the number of special positions in the matrix.

Examples

Example 1

Consider the matrix:

mat = [ [1, 0, 0], [0, 0, 1], [1, 0, 0] ]

The function numSpecial(mat) should return 1. This is because (1, 2) is a special position as mat[1][2] == 1 and all other elements in row 1 and column 2 are 0.

Example 2

Consider the matrix:

mat = [ [1, 0, 0], [0, 1, 0], [0, 0, 1] ]

The function numSpecial(mat) should return 3. This is because (0, 0), (1, 1), and (2, 2) are special positions.

Challenges

  1. Challenge 1: Export the numSpecial function for testing.
  2. Challenge 2: Implement numSpecial to find the count of special positions in the given matrix.
  3. Challenge 3: Optimize your solution, ensuring the implementation has a low time and space complexity.

Evaluation script

(Follow the given evaluation script format from the instructions to create the evaluation script, with try-catch blocks for each challenge.)

Setting up test environment script

(Keep the "Setting up test environment script" exactly as provided in the instructions.)

Initial file system for user

(Create the file system as shown in the instructions, using index.js and package.json files with correct configurations.)

Good luck and have fun developing and testing your coding skills in this lab!