1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
longest-line-of-consecutive-one-in-matrix.py 854 Bytes
一键复制 编辑 原始数据 按行查看 历史
# Time: O(m * n)
# Space: O(n)
class Solution(object):
def longestLine(self, M):
"""
:type M: List[List[int]]
:rtype: int
"""
if not M: return 0
result = 0
dp = [[[0] * 4 for _ in xrange(len(M[0]))] for _ in xrange(2)]
for i in xrange(len(M)):
for j in xrange(len(M[0])):
dp[i % 2][j][:] = [0] * 4
if M[i][j] == 1:
dp[i % 2][j][0] = dp[i % 2][j - 1][0]+1 if j > 0 else 1
dp[i % 2][j][1] = dp[(i-1) % 2][j][1]+1 if i > 0 else 1
dp[i % 2][j][2] = dp[(i-1) % 2][j-1][2]+1 if (i > 0 and j > 0) else 1
dp[i % 2][j][3] = dp[(i-1) % 2][j+1][3]+1 if (i > 0 and j < len(M[0])-1) else 1
result = max(result, max(dp[i % 2][j]))
return result
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助