-
Notifications
You must be signed in to change notification settings - Fork 508
/
Copy pathDistance of nearest cell having 1.cpp
91 lines (79 loc) · 2.2 KB
/
Distance of nearest cell having 1.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
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
/*
Distance of nearest cell having 1
=================================
Given a binary grid. Find the distance of nearest 1 in the grid for each cell.
The distance is calculated as |i1 – i2| + |j1 – j2|, where i1, j1 are the row number and column number of the current cell and i2, j2 are the row number and column number of the nearest cell having value 1.
Example 1:
Input: grid = {{0,1,1,0},{1,1,0,0},{0,0,1,1}}
Output: {{1,0,0,1},{0,0,1,1},{1,1,0,0}}
Explanation: The grid is-
0 1 1 0
1 1 0 0
0 0 1 1
0's at (0,0), (0,3), (1,2), (1,3), (2,0) and
(2,1) are at a distance of 1 from 1's at (0,1),
(0,2), (0,2), (2,3), (1,0) and (1,1)
respectively.
Example 2:
Input: grid = {{1,0,1},{1,1,0},{1,0,0}}
Output: {{0,1,0},{0,0,1},{0,1,2}}
Explanation: The grid is-
1 0 1
1 1 0
1 0 0
0's at (0,1), (1,2), (2,1) and (2,2) are at a
distance of 1, 1, 1 and 2 from 1's at (0,0),
(0,2), (2,0) and (1,1) respectively.
Yout Task:
You don't need to read or print anything, Your task is to complete the function nearest() which takes grid as input parameter and returns a matrix of same dimensions where the value at index (i, j) in the resultant matrix signifies the minimum distance of 1 in the matrix from grid[i][j].
Expected Time Complexity: O(n*m)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ n, m ≤ 500
*/
int dirs[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
vector<vector<int>> nearest(vector<vector<int>> grid)
{
int n = grid.size(), m = grid[0].size();
queue<vector<int>> q;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
if (grid[i][j] == 1)
{
q.push({i, j});
grid[i][j] = -1;
}
}
}
int dist = 0;
while (q.size())
{
dist++;
for (int i = q.size(); i > 0; --i)
{
auto curr = q.front();
q.pop();
int x = curr[0], y = curr[1];
for (auto &dir : dirs)
{
int nx = x + dir[0], ny = y + dir[1];
if (nx >= 0 && ny >= 0 && nx < n && ny < m && grid[nx][ny] == 0)
{
q.push({nx, ny});
grid[nx][ny] = dist;
}
}
}
}
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
if (grid[i][j] == -1)
grid[i][j] = 0;
}
}
return grid;
}