forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_764.java
132 lines (120 loc) · 4.77 KB
/
_764.java
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
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.fishercoder.solutions;
import java.util.HashSet;
import java.util.Set;
public class _764 {
public static class Solution1 {
/**
* Dp
* <p>
* Time: O(N^2)
* Space: O(N^2)
* Credit: https://leetcode.com/articles/largest-plus-sign/
*/
public int orderOfLargestPlusSign(int N, int[][] mines) {
Set<Integer> banned = new HashSet<>();
for (int[] mine : mines) {
banned.add(mine[0] * N + mine[1]);
}
int[][] dp = new int[N][N];
for (int row = 0; row < N; row++) {
int count = 0;
for (int col = 0; col < N; col++) {
count = banned.contains(row * N + col) ? 0 : count + 1;
dp[row][col] = count;
}
count = 0;
for (int col = N - 1; col >= 0; col--) {
count = banned.contains(row * N + col) ? 0 : count + 1;
dp[row][col] = Math.min(dp[row][col], count);
}
}
int result = 0;
for (int col = 0; col < N; col++) {
int count = 0;
for (int row = 0; row < N; row++) {
count = banned.contains(row * N + col) ? 0 : count + 1;
dp[row][col] = Math.min(dp[row][col], count);
}
count = 0;
for (int row = N - 1; row >= 0; row--) {
count = banned.contains(row * N + col) ? 0 : count + 1;
dp[row][col] = Math.min(dp[row][col], count);
result = Math.max(result, dp[row][col]);
}
}
return result;
}
}
public static class Solution2 {
/**
* credit: https://leetcode.com/problems/largest-plus-sign/discuss/113314/JavaC%2B%2BPython-O(N2)-solution-using-only-one-grid-matrix
*/
public int orderOfLargestPlusSign(int n, int[][] mines) {
int[][] grid = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
grid[i][j] = n;
}
}
for (int i = 0; i < mines.length; i++) {
grid[mines[i][0]][mines[i][1]] = 0;
}
for (int i = 0; i < n; i++) {
for (int j = 0, k = n - 1, l = 0, r = 0, u = 0, d = 0; j < n; j++, k--) {
grid[i][j] = Math.min(grid[i][j], l = (grid[i][j] == 0 ? 0 : l + 1));//left direction
grid[i][k] = Math.min(grid[i][k], r = (grid[i][k] == 0 ? 0 : r + 1));//right direction
grid[j][i] = Math.min(grid[j][i], u = (grid[j][i] == 0 ? 0 : u + 1));//upwards
grid[k][i] = Math.min(grid[k][i], d = (grid[k][i] == 0 ? 0 : d + 1));//downwards
}
}
int result = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
result = Math.max(result, grid[i][j]);
}
}
return result;
}
/**
* break the above into FOUR separate loops to go over four directions for easier understanding
*/
public int orderOfLargestPlusSign_initialVersion(int n, int[][] mines) {
int[][] grid = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
grid[i][j] = n;
}
}
for (int i = 0; i < mines.length; i++) {
grid[mines[i][0]][mines[i][1]] = 0;
}
for (int i = 0; i < n; i++) {
for (int j = 0, l = 0; j < n; j++) {
grid[i][j] = Math.min(grid[i][j], l = (grid[i][j] == 0 ? 0 : l + 1));
}
}
for (int i = 0; i < n; i++) {
for (int j = 0, k = n - 1, r = 0; j < n; j++, k--) {
grid[i][k] = Math.min(grid[i][k], r = (grid[i][k] == 0 ? 0 : r + 1));
}
}
for (int i = 0; i < n; i++) {
for (int j = 0, k = n - 1, u = 0; j < n; j++, k--) {
grid[j][i] = Math.min(grid[j][i], u = (grid[j][i] == 0 ? 0 : u + 1));
}
}
for (int i = 0; i < n; i++) {
for (int j = 0, k = n - 1, d = 0; j < n; j++, k--) {
grid[k][i] = Math.min(grid[k][i], d = (grid[k][i] == 0 ? 0 : d + 1));
}
}
int result = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
result = Math.max(result, grid[i][j]);
}
}
return result;
}
}
}