Advertisement
theinhumaneme

cells-with-odd-values-in-a-matrix

Jan 3rd, 2023
803
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.97 KB | None | 0 0
  1. // DATE: 03-01-2022
  2. // LINK: https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/submissions/870329177/
  3.  
  4.  
  5. class Solution {
  6.     public int oddCells(int m, int n, int[][] indices) {
  7.         int[][] finalMat = new int[m][n];
  8.         for( int[] arr: finalMat){
  9.             Arrays.fill(arr,0);
  10.         }
  11.         for(int[] index: indices){
  12.             finalMat = incrementRow(finalMat, index[0],n);
  13.             finalMat = incrementCol(finalMat, index[1],m);
  14.         }
  15.         int count=0;
  16.         for(int[] row: finalMat){
  17.             for(int col: row){
  18.                 if(col%2!=0) count++;
  19.             }
  20.         }
  21.         return count;
  22.     }
  23.     public int[][] incrementRow(int[][] arr, int row, int n){
  24.         for(int j=0;j<n;j++){
  25.             arr[row][j]+=1;
  26.         }
  27.         return arr;
  28.     }
  29.     public int[][] incrementCol(int[][] arr, int col, int m){
  30.         for(int i=0;i<m;i++){
  31.             arr[i][col]+=1;
  32.         }
  33.         return arr;
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement