3 ms·
It solves easy problems and gets nowhere close on hard problems. But I'm sure that's coming.
by heurist 4y ago
It solves easy problems and gets nowhere close on hard problems. But I'm sure that's coming.
- sayogo1227 4y agoHere is what I tried for (https://leetcode.com/problems/longest-increasing-path-in-a-matrix/ https://leetcode.com/problems/longest-increasing-path-in-a-m...) INPUT: Given an m x n integers matrix, return the length of the longest increasing path in matrix. From each cell, you can either move in four directions: left, right, up, or down. You may not move diagonally or move outside the boundary (i.e., wrap-around is not allowed). Input: matrix = [[9,9,4],[6,6,8],[2,1,1]] Output: 4 Explanation: The longest increasing path is [1, 2, 6, 9]. class Solution: def longestIncreasingPath(self, matrix: List[List[int]]) -> int: ChatGPT: if not matrix or not matrix[0]: return 0 # initialize the 2D array with all zeros dp = [[0] * len(matrix[0]) for _ in range(len(matrix))] # initialize the result with 0 result = 0 # iterate through each cell in the matrix for i in range(len(matrix)): for j in range(len(matrix[0])): # perform DFS starting from the current cell result = max(result, self.dfs(matrix, dp, i, j)) # return the result return result # helper function to perform DFS def dfs(self, matrix, dp, i, j): # if the path length is already calculated, return it if dp[i][j] > 0: return dp[i][j] # initialize the path length with 1 path_len = 1 # check the adjacent cells for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]: # get the coordinates of the adjacent cell x, y = i + dx, j + dy # check if the adjacent cell is valid if 0 <= x < len(matrix) and 0 <= y < len(matrix[0]): # check if the adjacent cell has a smaller value if matrix[x][y] > matrix[i][j]: # perform DFS on the adjacent cell and update the path length path_len = max(path_len, 1 + self.dfs(matrix, dp, x, y)) # store the path length in the 2D array dp[i][j] = path_len # return the path length return path_len
- DenisM 4y agoHave you tried submitting it?
- sayogo1227 4y agoYes. Accepted.
- DenisM 4y agoSo what do we make of that? A really good search engine, or an entity that can synthesize?