Cells with Odd Values in a Matrix - Array - Easy - LeetCode
💻 coding

Cells with Odd Values in a Matrix - Array - Easy - LeetCode

2 min read 462 words
2 min read
ShareWhatsAppPost on X
  • 1The problem involves incrementing all cells in specified rows and columns of a zero-initialized matrix.
  • 2After applying the increments, the task is to count how many cells have odd values in the final matrix.
  • 3The solution has a time complexity of O(n^2) and a space complexity of O(m*n) for storing the matrix.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"The problem involves incrementing all cells in specified rows and columns of a zero-initialized matrix."

Cells with Odd Values in a Matrix - Array - Easy - LeetCode

Given n and m which are the dimensions of a matrix initialized by zeros and given an array indices where indices[i] = [ri, ci]. For each pair of [ri, ci] you have to increment all cells in row ri and column ci by 1.

Return the number of cells with odd values in the matrix after applying the increment to all indices.

Example 1:

Input: n = 2, m = 3, indices = [[0,1],[1,1]]

Output: 6

Explanation: Initial matrix = [[0,0,0],[0,0,0]].

After applying first increment it becomes [[1,2,1],[0,1,0]].

The final matrix will be [[1,3,1],[1,3,1]] which contains 6 odd numbers.

Example 2:

Input: n = 2, m = 2, indices = [[1,1],[0,0]]

Output: 0

Explanation: Final matrix = [[2,2],[2,2]]. There is no odd number in the final matrix.

Constraints:

1 <= n <= 50

1 <= m <= 50

1 <= indices.length <= 100

0 <= indices[i][0] < n

0 <= indices[i][1] < m

Solution:

using System;
using System.Collections.Generic;
using System.Text;

namespace LeetCode.AskGif.Easy.Array
{
 public class OddCellsSoln
 {
 public int OddCells(int n, int m, int[][] indices)
 {
 var matrix = new int[n, m];

 //initialize matrix to zero.
 for (int i = 0; i < n; i++)
 {
 for (int j = 0; j < m; j++)
 {
 matrix[i, j] = 0;
 }
 }

 for (int i = 0; i < indices.Length; i++)
 {
 int x = indices[i][0];
 int y = indices[i][1];

 //increment x axis
 for (int a = 0; a < m; a++)
 {
 matrix[x,a]++;
 }

 //increment y axis
 for (int b = 0; b < n; b++)
 {
 matrix[b, y]++;
 }
 }

 int odd = 0;
 for (int i = 0; i < n; i++)
 {
 for (int j = 0; j < m; j++)
 {
 if (matrix[i, j] % 2 != 0)
 {
 odd++;
 }
 }
 }

 return odd;
 }
 }
}

Time Complexity: O(n^2)

Space Complexity: O(m*n) For storing the result

Unit Tests:

using LeetCode.AskGif.Easy.Array;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Text;

namespace CodingUnitTest.Easy.Array
{
 [TestClass]
 public class OddCellsSolnTests
 {
 [TestMethod]
 public void OddCellsSoln_First()
 {
 var indices = new int[,] {
 { 0, 1 },
 { 1, 1 }
 };
 var n = 2;
 var m = 3;
 var output = 6;

 var res = new OddCellsSoln().OddCells(n, m, ArrayMapper(indices));

 Assert.AreEqual(output, res);
 }

 [TestMethod]
 public void OddCellsSoln_Second()
 {
 var indices = new int[,] {
 { 1, 1 },
 { 0, 0 }
 };
 var n = 2;
 var m = 2;
 var output = 0;

 var res = new OddCellsSoln().OddCells(n, m, ArrayMapper(indices));

 Assert.AreEqual(output, res);
 }

 private int[][] ArrayMapper(int[,] matrix)
 {
 var arr = new int[matrix.GetLength(0)][];
 for (int i = 0; i < matrix.GetLength(0); i++)
 {
 arr[i] = new int[matrix.GetLength(1)];
 for (int j = 0; j < matrix.GetLength(1); j++)
 {
 arr[i][j] = matrix[i, j];
 }
 }

 return arr;
 }
 }
}

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 11 June 2020 · 2 min read · 462 words

Part of AskGif Blog · coding

You might also like