769. Largest Plus Sign
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the largest axis-aligned plus sign of 1s in a grid. The grid is initially all 1s, with some cells turned to 0s (mines). The size of a plus sign is defined by its "order" k, which means it has a center at grid[r][c] == 1 and four arms of length k-1 extending up, down, left, and right, all composed of 1s. A brute-force approach, checking every cell as a potential center and expanding outwards, would be O(n^3), which is likely too slow given n up to 500.
Solution
Largest Plus Sign in Python
class Solution:
def orderOfLargestPlusSign(self, n: int, mines: List[List[int]]) -> int:
# Use a set for O(1) lookups of mine locations.
banned = {tuple(mine) for mine in mines}
# dp[r][c] will store the order of the plus sign centered at (r, c).
dp = [[0] * n for _ in range(n)]
ans = 0
# Left to Right pass
# For each cell, calculate the length of consecutive 1s to its left.
for r in range(n):
count = 0
for c in range(n):
count = 0 if (r, c) in banned else count + 1
dp[r][c] = count
# Right to Left pass
# Calculate length of 1s to the right and take the minimum with the left pass result.
for r in range(n):
count = 0
for c in range(n - 1, -1, -1):
count = 0 if (r, c) in banned else count + 1
dp[r][c] = min(dp[r][c], count)
# Top to Bottom pass
# Calculate length of 1s above and take the minimum.
for c in range(n):
count = 0
for r in range(n):
count = 0 if (r, c) in banned else count + 1
dp[r][c] = min(dp[r][c], count)
# Bottom to Top pass
# Calculate length of 1s below, take minimum, and find the overall max order.
for c in range(n):
count = 0
for r in range(n - 1, -1, -1):
count = 0 if (r, c) in banned else count + 1
dp[r][c] = min(dp[r][c], count)
ans = max(ans, dp[r][c])
return ans
Largest Plus Sign in Java
class Solution {
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];
int ans = 0;
int count;
// Left to Right (calculating left arm length)
for (int r = 0; r < n; r++) {
count = 0;
for (int c = 0; c < n; c++) {
count = banned.contains(r * n + c) ? 0 : count + 1;
dp[r][c] = count;
}
}
// Right to Left (calculating right arm length and taking min with left)
for (int r = 0; r < n; r++) {
count = 0;
for (int c = n - 1; c >= 0; c--) {
count = banned.contains(r * n + c) ? 0 : count + 1;
dp[r][c] = Math.min(dp[r][c], count);
}
}
// Top to Bottom (calculating up arm length and taking min with horizontal)
for (int c = 0; c < n; c++) {
count = 0;
for (int r = 0; r < n; r++) {
count = banned.contains(r * n + c) ? 0 : count + 1;
dp[r][c] = Math.min(dp[r][c], count);
}
}
// Bottom to Top (calculating down arm length and finding the max order)
for (int c = 0; c < n; c++) {
count = 0;
for (int r = n - 1; r >= 0; r--) {
count = banned.contains(r * n + c) ? 0 : count + 1;
dp[r][c] = Math.min(dp[r][c], count);
ans = Math.max(ans, dp[r][c]);
}
}
return ans;
}
}
Largest Plus Sign in C++
class Solution {
public:
int orderOfLargestPlusSign(int n, vector<vector<int>>& mines) {
unordered_set<int> banned;
for (const auto& mine : mines) {
banned.insert(mine[0] * n + mine[1]);
}
vector<vector<int>> dp(n, vector<int>(n, 0));
int ans = 0;
// Left to Right
for (int r = 0; r < n; ++r) {
int count = 0;
for (int c = 0; c < n; ++c) {
count = banned.count(r * n + c) ? 0 : count + 1;
dp[r][c] = count;
}
}
// Right to Left
for (int r = 0; r < n; ++r) {
int count = 0;
for (int c = n - 1; c >= 0; --c) {
count = banned.count(r * n + c) ? 0 : count + 1;
dp[r][c] = min(dp[r][c], count);
}
}
// Top to Bottom
for (int c = 0; c < n; ++c) {
int count = 0;
for (int r = 0; r < n; ++r) {
count = banned.count(r * n + c) ? 0 : count + 1;
dp[r][c] = min(dp[r][c], count);
}
}
// Bottom to Top
for (int c = 0; c < n; ++c) {
int count = 0;
for (int r = n - 1; r >= 0; --r) {
count = banned.count(r * n + c) ? 0 : count + 1;
dp[r][c] = min(dp[r][c], count);
ans = max(ans, dp[r][c]);
}
}
return ans;
}
};
Largest Plus Sign in TypeScript
function orderOfLargestPlusSign(n: number, mines: number[][]): number {
const banned = new Set<number>();
for (const mine of mines) {
banned.add(mine[0] * n + mine[1]);
}
const dp: number[][] = Array(n).fill(0).map(() => Array(n).fill(0));
let ans = 0;
let count: number;
// Left to Right
for (let r = 0; r < n; r++) {
count = 0;
for (let c = 0; c < n; c++) {
count = banned.has(r * n + c) ? 0 : count + 1;
dp[r][c] = count;
}
}
// Right to Left
for (let r = 0; r < n; r++) {
count = 0;
for (let c = n - 1; c >= 0; c--) {
count = banned.has(r * n + c) ? 0 : count + 1;
dp[r][c] = Math.min(dp[r][c], count);
}
}
// Top to Bottom
for (let c = 0; c < n; c++) {
count = 0;
for (let r = 0; r < n; r++) {
count = banned.has(r * n + c) ? 0 : count + 1;
dp[r][c] = Math.min(dp[r][c], count);
}
}
// Bottom to Top
for (let c = 0; c < n; c++) {
count = 0;
for (let r = n - 1; r >= 0; r--) {
count = banned.has(r * n + c) ? 0 : count + 1;
dp[r][c] = Math.min(dp[r][c], count);
ans = Math.max(ans, dp[r][c]);
}
}
return ans;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.