Skip to content

Commit 2916fe0

Browse files
authored
Added task 764.
1 parent ce8db7a commit 2916fe0

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g0701_0800.s0764_largest_plus_sign;
2+
3+
// #Medium #Array #Dynamic_Programming
4+
5+
public class Solution {
6+
public int orderOfLargestPlusSign(int n, int[][] mines) {
7+
boolean[][] mat = new boolean[n][n];
8+
for (int[] pos : mines) {
9+
mat[pos[0]][pos[1]] = true;
10+
}
11+
int[][] left = new int[n][n];
12+
int[][] right = new int[n][n];
13+
int[][] up = new int[n][n];
14+
int[][] down = new int[n][n];
15+
int ans = 0;
16+
// For Left and Up only
17+
for (int i = 0; i < n; i++) {
18+
for (int j = 0; j < n; j++) {
19+
int i1 = j == 0 ? 0 : left[i][j - 1];
20+
left[i][j] = mat[i][j] ? 0 : 1 + i1;
21+
int i2 = i == 0 ? 0 : up[i - 1][j];
22+
up[i][j] = mat[i][j] ? 0 : 1 + i2;
23+
}
24+
}
25+
// For Right and Down and simoultaneously get answer
26+
for (int i = n - 1; i >= 0; i--) {
27+
for (int j = n - 1; j >= 0; j--) {
28+
int i1 = j == n - 1 ? 0 : right[i][j + 1];
29+
right[i][j] = mat[i][j] ? 0 : 1 + i1;
30+
int i2 = i == n - 1 ? 0 : down[i + 1][j];
31+
down[i][j] = mat[i][j] ? 0 : 1 + i2;
32+
int x = Math.min(Math.min(left[i][j], up[i][j]), Math.min(right[i][j], down[i][j]));
33+
ans = Math.max(ans, x);
34+
}
35+
}
36+
return ans;
37+
}
38+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
764\. Largest Plus Sign
2+
3+
Medium
4+
5+
You are given an integer `n`. You have an `n x n` binary grid `grid` with all values initially `1`'s except for some indices given in the array `mines`. The <code>i<sup>th</sup></code> element of the array `mines` is defined as <code>mines[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> where <code>grid[x<sub>i</sub>][y<sub>i</sub>] == 0</code>.
6+
7+
Return _the order of the largest **axis-aligned** plus sign of_ 1_'s contained in_ `grid`. If there is none, return `0`.
8+
9+
An **axis-aligned plus sign** of `1`'s of order `k` has some center `grid[r][c] == 1` along with four arms of length `k - 1` going up, down, left, and right, and made of `1`'s. Note that there could be `0`'s or `1`'s beyond the arms of the plus sign, only the relevant area of the plus sign is checked for `1`'s.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2021/06/13/plus1-grid.jpg)
14+
15+
**Input:** n = 5, mines = [[4,2]]
16+
17+
**Output:** 2
18+
19+
**Explanation:** In the above grid, the largest plus sign can only be of order 2. One of them is shown.
20+
21+
**Example 2:**
22+
23+
![](https://assets.leetcode.com/uploads/2021/06/13/plus2-grid.jpg)
24+
25+
**Input:** n = 1, mines = [[0,0]]
26+
27+
**Output:** 0
28+
29+
**Explanation:** There is no plus sign, so return 0.
30+
31+
**Constraints:**
32+
33+
* `1 <= n <= 500`
34+
* `1 <= mines.length <= 5000`
35+
* <code>0 <= x<sub>i</sub>, y<sub>i</sub> < n</code>
36+
* All the pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are **unique**.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0701_0800.s0764_largest_plus_sign;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void orderOfLargestPlusSign() {
11+
assertThat(new Solution().orderOfLargestPlusSign(5, new int[][] {{4, 2}}), equalTo(2));
12+
}
13+
14+
@Test
15+
void orderOfLargestPlusSign2() {
16+
assertThat(new Solution().orderOfLargestPlusSign(1, new int[][] {{0, 0}}), equalTo(0));
17+
}
18+
}

0 commit comments

Comments
 (0)