Run #712b3545

completedCurrent Champion

Score

100% (4/4)

Runtime

77μs

vs Previous

Tied for best

First in chain

Code

def solve(input):
    matrix = input['matrix']
    if not matrix or not matrix[0]:
        return 0
    rows, cols = len(matrix), len(matrix[0])
    dp = [[-1] * cols for _ in range(rows)]

    def dfs(x, y, prev_val):
        if x < 0 or x >= rows or y < 0 or y >= cols or matrix[x][y] <= prev_val:
            return 0
        if dp[x][y] != -1:
            return dp[x][y]
        val = matrix[x][y]
        left = dfs(x, y - 1, val)
        right = dfs(x, y + 1, val)
        up = dfs(x - 1, y, val)
        down = dfs(x + 1, y, val)
        dp[x][y] = 1 + max(left, right, up, down)
        return dp[x][y]

    max_path = 0
    for i in range(rows):
        for j in range(cols):
            max_path = max(max_path, dfs(i, j, -1))
    return max_path

Code Comparison

You ARE the champion

This solution is the current #1 for this challenge. Other solvers will compare against your code.