Skip to content

Commit

Permalink
leetcode.com 329. Longest Increasing Path in a Matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
skysign committed Jul 12, 2024
1 parent 1077933 commit dc6562b
Showing 1 changed file with 41 additions and 38 deletions.
79 changes: 41 additions & 38 deletions leetcode.com 329. Longest Increasing Path in a Matrix/main.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,44 @@
from typing import List

class Solution:
def longestIncreasingPath(self, dt: List[List[int]]) -> int:
dp = [[-1 for _ in range(len(dt[0]))] for _ in range(len(dt))]

for sy in range(len(dt)):
for sx in range(len(dt[0])):
self.dfs(sy, sx, 1, dp, dt)

answer = 0

for sy in range(len(dt)):
for sx in range(len(dt[0])):
answer = max(answer, dp[sy][sx])

return answer

def dfs(self, sy, sx, depth, dp, dt):
if dp[sy][sx] >= depth:
return

dp[sy][sx] = depth

dyxs = [(-1, 0), (0, 1), (1, 0), (0, -1)]
for dyx in dyxs:
dy, dx = dyx
ty = sy + dy
tx = sx + dx

if self.is_in(ty, tx, dt):
if dt[sy][sx] < dt[ty][tx]:
if dp[ty][tx] < depth +1:
self.dfs(ty, tx, depth +1, dp, dt)


def is_in(self, idx_row, idx_col, map):
if (0 <= idx_row < len(map)) and (0 <= idx_col < len(map[0])):
return True

return False
class Solution:
def __init__(self):
self.drc = [[-1, 0], [0, 1], [1, 0], [0, -1]]
self.mx_row = 0
self.mx_col = 0

def longestIncreasingPath(self, matrix: List[List[int]]) -> int:
self.mx_row = len(matrix)
self.mx_col = len(matrix[0])
outdegree: List[List[int]] = [[0 for _ in range(self.mx_col)] for _ in range(self.mx_row)]
leaves = []

for r in range(self.mx_row):
for c in range(self.mx_col):
for dr, dc in self.drc:
nr, nc = r + dr, c + dc
if 0 <= nr < self.mx_row and 0 <= nc < self.mx_col:
if matrix[nr][nc] > matrix[r][c]:
outdegree[r][c] += 1

if outdegree[r][c] == 0:
leaves.append([r, c])

mx = 0
while len(leaves) > 0:
mx += 1
newLeaves = []

for r, c in leaves:
for dr, dc in self.drc:
nr, nc = r + dr, c + dc
if 0 <= nr < self.mx_row and 0 <= nc < self.mx_col:
if matrix[nr][nc] < matrix[r][c]:
outdegree[nr][nc] -= 1

if outdegree[nr][nc] == 0:
newLeaves.append([nr, nc])

leaves = newLeaves

return mx

0 comments on commit dc6562b

Please sign in to comment.