-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01 Matrix
More file actions
119 lines (106 loc) · 3.6 KB
/
01 Matrix
File metadata and controls
119 lines (106 loc) · 3.6 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
Given an m x n binary matrix mat, return the distance of the nearest 0 for each cell.
The distance between two cells sharing a common edge is 1.
Example 1:
Input: mat = [[0,0,0],[0,1,0],[0,0,0]]
Output: [[0,0,0],[0,1,0],[0,0,0]]
Example 2:
Input: mat = [[0,0,0],[0,1,0],[1,1,1]]
Output: [[0,0,0],[0,1,0],[1,2,1]]
----------------------------------------------------------------------------------------------------------------------------------------------
// tc=O(n*m) c=O(n*m)
class Solution {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {
int n = mat.size();
int m = mat[0].size();
vector<vector<int>> dist(n, vector<int>(m, INT_MAX));
queue<pair<int, int>> q;
// Step 1: Push all 0s into the queue
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (mat[i][j] == 0) {
dist[i][j] = 0;
q.push({i, j});
}
}
}
int drow[] = {-1, 0, 1, 0};
int dcol[] = {0, 1, 0, -1};
// Step 2: BFS traversal
while (!q.empty()) {
auto [row, col] = q.front();
q.pop();
for (int k = 0; k < 4; k++) {
int nRow = row + drow[k];
int nCol = col + dcol[k];
if (nRow >= 0 && nRow < n && nCol >= 0 && nCol < m) {
if (dist[nRow][nCol] > dist[row][col] + 1) {
dist[nRow][nCol] = dist[row][col] + 1;
q.push({nRow, nCol});
}
}
}
}
// Step 3: Replace distance = 2 with value 2 in the matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (dist[i][j] == 2)
mat[i][j] = 2;
else
mat[i][j] = dist[i][j];
}
}
--------------------------------------------------------------------------------------------------------
//dfs tc=O(n*m) sc=O(n*m)
class Solution {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {
int n = mat.size();
int m = mat[0].size();
// visited and distance matrix
vector<vector<int>> vis(n, vector<int>(m, 0));
vector<vector<int>> dist(n, vector<int>(m, 0));
// <coordinates, steps>
queue<pair<pair<int,int>, int>> q;
// traverse the matrix
for(int i = 0;i<n;i++) {
for(int j = 0;j<m;j++) {
// start BFS if cell contains 1
if(mat[i][j] == 0) {
q.push({{i,j}, 0});
vis[i][j] = 1;
}
else {
// mark unvisited
vis[i][j] = 0;
}
}
}
int delrow[] = {-1, 0, +1, 0};
int delcol[] = {0, +1, 0, -1};
// traverse till queue becomes empty
while(!q.empty()) {
int row = q.front().first.first;
int col = q.front().first.second;
int steps = q.front().second;
q.pop();
dist[row][col] = steps;
// for all 4 neighbours
for(int i = 0;i<4;i++) {
int nrow = row + delrow[i];
int ncol = col + delcol[i];
// check for valid unvisited cell
if(nrow >= 0 && nrow < n && ncol >= 0 && ncol < m
&& vis[nrow][ncol] == 0) {
vis[nrow][ncol] = 1;
q.push({{nrow, ncol}, steps+1});
}
}
}
// return distance matrix
return dist;
}
};
return mat;
}
};