-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoverageOfMatrix.cpp
48 lines (38 loc) · 1 KB
/
CoverageOfMatrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//////////////////////////////////////CODE STUDIO////////////////////////////////////
// EASY
/*
In a matrix, check the num of 1's surrounding the 0's , return their sum
*/
/*
SOL:
#include <bits/stdc++.h>
int coverageOfMatrix(vector<vector<int>> &mat) {
// Write your code here.
int count=0;
int n=mat.size();
int m=mat[0].size();
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(mat[i][j]==0){
//check for 1's in left
if((mat[i][j-1]==1) && (j>0))
count++;
//check for 1's in right
if((mat[i][j+1]==1) && (j<=m))
count++;
//check for 1's in up
if((mat[i-1][j]==1) && (i>0))
count++;
//check for 1's in down
if((mat[i+1][j]==1) && (i<=n))
count++;
}else{
continue;
}
}
}
return count;
}
TC: O(n square)
SC: O(1)
*/