Cells with Odd Values in a Matrix(LeetCode-easy)
INTRODUCTION
We, have given two array one is main array matrix and other is indices[is a 2D array]. Each indices value consist two things one is row value and another is column value, indices[i] = [ri, ci]. And our task is to simply increment by 1 the whole row and whole column.
NOTE: Because indices is 2D array, so we can easily get the row value using row = indices[i][0] , and the column value using col = indices[i][1]
Examples
Hints
Steps
Create an array of size m, n
Set the row value and col value from indices
Increment the whole row and col by 1
Check the main matrix values for ODD number.
Return the counter.
Java Code
class Solution {
public int oddCells(int m, int n, int[][] indices) {
int[][] mat = new int[m][n];
for (int i = 0; i < indices.length; i++) {
int row = indices[i][0];
int col = indices[i][1];
for (int j = 0; j <n; j++) {
mat[row][j]++;
}
for (int j = 0; j <m; j++) {
mat[j][col]++;
}
}
int count = 0;
for (int i = 0; i <m; i++) {
for (int j = 0; j < n; j++) {
if(mat[i][j] % 2 != 0){
count++;
}
}
}
return count;
}
}
Output
Subscribe to my newsletter
Read articles from Tahzib Mahmud Rifat directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Tahzib Mahmud Rifat
Tahzib Mahmud Rifat
Undergrad Student , interested in Ai, robotics , Internet of Things. Love to contribute in opensource projects