-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathas_far_from_land_as_possible.dart
115 lines (96 loc) · 2.73 KB
/
as_far_from_land_as_possible.dart
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
/*
-* 1162. As Far from Land as Possible *-
Given an n x n grid containing only values 0 and 1, where 0 represents water and 1 represents land, find a water cell such that its distance to the nearest land cell is maximized, and return the distance. If no land or water exists in the grid, return -1.
The distance used in this problem is the Manhattan distance: the distance between two cells (x0, y0) and (x1, y1) is |x0 - x1| + |y0 - y1|.
Example 1:
Input: grid = [[1,0,1],[0,0,0],[1,0,1]]
Output: 2
Explanation: The cell (1, 1) is as far as possible from all the land with distance 2.
Example 2:
Input: grid = [[1,0,0],[0,0,0],[0,0,0]]
Output: 4
Explanation: The cell (2, 2) is as far as possible from all the land with distance 4.
Constraints:
n == grid.length
n == grid[i].length
1 <= n <= 100
grid[i][j] is 0 or 1
*/
import 'dart:collection';
import 'dart:math';
// TLE
class A {
int maxDistance(List<List<int>> grid) {
int n = grid.length;
Queue<List<int>> q = Queue();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) q.add([i, j]);
}
}
if (q.isEmpty || q.length == n * n) return -1;
int res = -1;
List<List<int>> dirs = [
[0, 1],
[0, -1],
[1, 0],
[-1, 0]
];
while (q.isNotEmpty) {
int size = q.length;
res++;
while (size-- > 0) {
// poll == first
List<int> cell = q.first;
int x = cell[0], y = cell[1];
for (List<int> dir in dirs) {
int i = x + dir[0], j = y + dir[1];
if (i >= 0 && i < n && j >= 0 && j < n && grid[i][j] == 0) {
grid[i][j] = 1;
q.add([i, j]);
}
}
}
}
return res;
}
}
class B {
int maxDistance(List<List<int>> grid) {
int m = grid.length;
int n = grid[0].length;
int x = n + m;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
continue;
}
int top = x;
int left = x;
if (i - 1 >= 0) top = grid[i - 1][j];
if (j - 1 >= 0) left = grid[i][j - 1];
grid[i][j] = min(top, left) + 1;
}
}
for (int i = m - 1; i >= 0; i--) {
for (int j = n - 1; j >= 0; j--) {
if (grid[i][j] == 1) {
continue;
}
int bottom = x;
int right = x;
if (i + 1 < m) bottom = grid[i + 1][j];
if (j + 1 < n) right = grid[i][j + 1];
grid[i][j] = min(grid[i][j], min(bottom, right) + 1);
}
}
// int.MinValue
int count = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
count = max(count, grid[i][j]);
}
}
return count - 1 == n + m + 1 || count - 1 == 0 ? -1 : count - 1;
}
}