返回介绍

solution / 0500-0599 / 0562.Longest Line of Consecutive One in Matrix / README_EN

发布于 2024-06-17 01:03:59 字数 4665 浏览 0 评论 0 收藏 0

562. Longest Line of Consecutive One in Matrix

中文文档

Description

Given an m x n binary matrix mat, return _the length of the longest line of consecutive one in the matrix_.

The line could be horizontal, vertical, diagonal, or anti-diagonal.

 

Example 1:

Input: mat = [[0,1,1,0],[0,1,1,0],[0,0,0,1]]
Output: 3

Example 2:

Input: mat = [[1,1,1,1],[0,1,1,0],[0,0,0,1]]
Output: 4

 

Constraints:

  • m == mat.length
  • n == mat[i].length
  • 1 <= m, n <= 104
  • 1 <= m * n <= 104
  • mat[i][j] is either 0 or 1.

Solutions

Solution 1

class Solution:
  def longestLine(self, mat: List[List[int]]) -> int:
    m, n = len(mat), len(mat[0])
    a = [[0] * (n + 2) for _ in range(m + 2)]
    b = [[0] * (n + 2) for _ in range(m + 2)]
    c = [[0] * (n + 2) for _ in range(m + 2)]
    d = [[0] * (n + 2) for _ in range(m + 2)]
    ans = 0
    for i in range(1, m + 1):
      for j in range(1, n + 1):
        if mat[i - 1][j - 1]:
          a[i][j] = a[i - 1][j] + 1
          b[i][j] = b[i][j - 1] + 1
          c[i][j] = c[i - 1][j - 1] + 1
          d[i][j] = d[i - 1][j + 1] + 1
          ans = max(ans, a[i][j], b[i][j], c[i][j], d[i][j])
    return ans
class Solution {
  public int longestLine(int[][] mat) {
    int m = mat.length, n = mat[0].length;
    int[][] a = new int[m + 2][n + 2];
    int[][] b = new int[m + 2][n + 2];
    int[][] c = new int[m + 2][n + 2];
    int[][] d = new int[m + 2][n + 2];
    int ans = 0;
    for (int i = 1; i <= m; ++i) {
      for (int j = 1; j <= n; ++j) {
        if (mat[i - 1][j - 1] == 1) {
          a[i][j] = a[i - 1][j] + 1;
          b[i][j] = b[i][j - 1] + 1;
          c[i][j] = c[i - 1][j - 1] + 1;
          d[i][j] = d[i - 1][j + 1] + 1;
          ans = max(ans, a[i][j], b[i][j], c[i][j], d[i][j]);
        }
      }
    }
    return ans;
  }

  private int max(int... arr) {
    int ans = 0;
    for (int v : arr) {
      ans = Math.max(ans, v);
    }
    return ans;
  }
}
class Solution {
public:
  int longestLine(vector<vector<int>>& mat) {
    int m = mat.size(), n = mat[0].size();
    vector<vector<int>> a(m + 2, vector<int>(n + 2));
    vector<vector<int>> b(m + 2, vector<int>(n + 2));
    vector<vector<int>> c(m + 2, vector<int>(n + 2));
    vector<vector<int>> d(m + 2, vector<int>(n + 2));
    int ans = 0;
    for (int i = 1; i <= m; ++i) {
      for (int j = 1; j <= n; ++j) {
        if (mat[i - 1][j - 1]) {
          a[i][j] = a[i - 1][j] + 1;
          b[i][j] = b[i][j - 1] + 1;
          c[i][j] = c[i - 1][j - 1] + 1;
          d[i][j] = d[i - 1][j + 1] + 1;
          ans = max(ans, max(a[i][j], max(b[i][j], max(c[i][j], d[i][j]))));
        }
      }
    }
    return ans;
  }
};
func longestLine(mat [][]int) (ans int) {
  m, n := len(mat), len(mat[0])
  f := make([][][4]int, m+2)
  for i := range f {
    f[i] = make([][4]int, n+2)
  }
  for i := 1; i <= m; i++ {
    for j := 1; j <= n; j++ {
      if mat[i-1][j-1] == 1 {
        f[i][j][0] = f[i-1][j][0] + 1
        f[i][j][1] = f[i][j-1][1] + 1
        f[i][j][2] = f[i-1][j-1][2] + 1
        f[i][j][3] = f[i-1][j+1][3] + 1
        for _, v := range f[i][j] {
          if ans < v {
            ans = v
          }
        }
      }
    }
  }
  return
}

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

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

发布评论

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