返回介绍

lcci / 17.23.Max Black Square / README_EN

发布于 2024-06-17 01:04:42 字数 6092 浏览 0 评论 0 收藏 0

17.23. Max Black Square

中文文档

Description

Imagine you have a square matrix, where each cell (pixel) is either black or white Design an algorithm to find the maximum subsquare such that all four borders are filled with black pixels.

Return an array [r, c, size], where rc are the row number and the column number of the subsquare's upper left corner respectively, and size is the side length of the subsquare. If there are more than one answers, return the one that has smallest r. If there are more than one answers that have the same r, return the one that has smallest c. If there's no answer, return an empty array.

Example 1:


Input:

[

  [1,0,1],

  [0,0,1],

  [0,0,1]

]

Output: [1,0,2]

Explanation: 0 represents black, and 1 represents white, bold elements in the input is the answer.

Example 2:


Input:

[

  [0,1,1],

  [1,0,1],

  [1,1,0]

]

Output: [0,0,1]

Note:

  • matrix.length == matrix[0].length <= 200

Solutions

Solution 1

class Solution:
  def findSquare(self, matrix: List[List[int]]) -> List[int]:
    n = len(matrix)
    down = [[0] * n for _ in range(n)]
    right = [[0] * n for _ in range(n)]
    for i in range(n - 1, -1, -1):
      for j in range(n - 1, -1, -1):
        if matrix[i][j] == 0:
          down[i][j] = down[i + 1][j] + 1 if i + 1 < n else 1
          right[i][j] = right[i][j + 1] + 1 if j + 1 < n else 1
    for k in range(n, 0, -1):
      for i in range(n - k + 1):
        for j in range(n - k + 1):
          if (
            down[i][j] >= k
            and right[i][j] >= k
            and right[i + k - 1][j] >= k
            and down[i][j + k - 1] >= k
          ):
            return [i, j, k]
    return []
class Solution {
  public int[] findSquare(int[][] matrix) {
    int n = matrix.length;
    int[][] down = new int[n][n];
    int[][] right = new int[n][n];
    for (int i = n - 1; i >= 0; --i) {
      for (int j = n - 1; j >= 0; --j) {
        if (matrix[i][j] == 0) {
          down[i][j] = i + 1 < n ? down[i + 1][j] + 1 : 1;
          right[i][j] = j + 1 < n ? right[i][j + 1] + 1 : 1;
        }
      }
    }
    for (int k = n; k > 0; --k) {
      for (int i = 0; i <= n - k; ++i) {
        for (int j = 0; j <= n - k; ++j) {
          if (down[i][j] >= k && right[i][j] >= k && right[i + k - 1][j] >= k
            && down[i][j + k - 1] >= k) {
            return new int[] {i, j, k};
          }
        }
      }
    }
    return new int[0];
  }
}
class Solution {
public:
  vector<int> findSquare(vector<vector<int>>& matrix) {
    int n = matrix.size();
    int down[n][n];
    int right[n][n];
    memset(down, 0, sizeof(down));
    memset(right, 0, sizeof(right));
    for (int i = n - 1; i >= 0; --i) {
      for (int j = n - 1; j >= 0; --j) {
        if (matrix[i][j] == 0) {
          down[i][j] = i + 1 < n ? down[i + 1][j] + 1 : 1;
          right[i][j] = j + 1 < n ? right[i][j + 1] + 1 : 1;
        }
      }
    }
    for (int k = n; k > 0; --k) {
      for (int i = 0; i <= n - k; ++i) {
        for (int j = 0; j <= n - k; ++j) {
          if (down[i][j] >= k && right[i][j] >= k && right[i + k - 1][j] >= k && down[i][j + k - 1] >= k) {
            return {i, j, k};
          }
        }
      }
    }
    return {};
  }
};
func findSquare(matrix [][]int) []int {
  n := len(matrix)
  down := make([][]int, n)
  right := make([][]int, n)
  for i := range down {
    down[i] = make([]int, n)
    right[i] = make([]int, n)
  }
  for i := n - 1; i >= 0; i-- {
    for j := n - 1; j >= 0; j-- {
      if matrix[i][j] == 0 {
        down[i][j], right[i][j] = 1, 1
        if i+1 < n {
          down[i][j] += down[i+1][j]
        }
        if j+1 < n {
          right[i][j] += right[i][j+1]
        }
      }
    }
  }
  for k := n; k > 0; k-- {
    for i := 0; i <= n-k; i++ {
      for j := 0; j <= n-k; j++ {
        if down[i][j] >= k && right[i][j] >= k && right[i+k-1][j] >= k && down[i][j+k-1] >= k {
          return []int{i, j, k}
        }
      }
    }
  }
  return []int{}
}
function findSquare(matrix: number[][]): number[] {
  const n = matrix.length;
  const down: number[][] = new Array(n).fill(0).map(() => new Array(n).fill(0));
  const right: number[][] = new Array(n).fill(0).map(() => new Array(n).fill(0));
  for (let i = n - 1; i >= 0; --i) {
    for (let j = n - 1; j >= 0; --j) {
      if (matrix[i][j] === 0) {
        down[i][j] = i + 1 < n ? down[i + 1][j] + 1 : 1;
        right[i][j] = j + 1 < n ? right[i][j + 1] + 1 : 1;
      }
    }
  }
  for (let k = n; k > 0; --k) {
    for (let i = 0; i <= n - k; ++i) {
      for (let j = 0; j <= n - k; ++j) {
        if (
          down[i][j] >= k &&
          right[i][j] >= k &&
          right[i + k - 1][j] >= k &&
          down[i][j + k - 1] >= k
        ) {
          return [i, j, k];
        }
      }
    }
  }
  return [];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文