python - 循环总是从 0,0 开始 - 试图解决“矩阵中最长的增加路径”。我没看到什么?
问题描述
我一直在努力解决这个 leetcode 问题:https ://leetcode.com/problems/longest-increasing-path-in-a-matrix/
我的代码在这里:https ://pastebin.com/QBcXrbJa
问题基本上是要求给定矩阵的最长“路径”。我已经添加了我期望的代码想要的
我们在solver()里面有一个isValid函数来检查
- 当前行/列在矩阵内
- 当前节点值大于上一个
- 我们以前没见过
如果它检查出来,numPath(计算路径长度的变量)会增加,我们将节点添加到visited,并继续检查它周围的所有节点
然而....
我得到一个不正确的输出(例如,矩阵 [[9,9,4],[6,6,8],[2,1,1]] 输出 2 而不是预期的输出 4)
从我的调试代码中,我注意到前 4 个访问的始终是 (0,0),即使我们不是从 (0, 0) 开始
例如,在 main 函数中,我们有
for rowKey in range(0, matrixR):
for colKey in range(0, matrixC):
longest.append(
solver(matrix, rowKey, colKey, matrixR, matrixC, matrix[rowKey][colKey])
)
这应该改变代码的开始位置,但这并没有反映在调试代码中
调试输出
Row: 0 - Col: 0 - RowMax: 3 - ColMax: 3 - currVal: 9 - numPath: 0 - visited: []
Row: 1 - Col: 0 - RowMax: 3 - ColMax: 3 - currVal: 9 - numPath: 1 - visited: [(0, 0)]
Row: -1 - Col: 0 - RowMax: 3 - ColMax: 3 - currVal: 9 - numPath: 1 - visited: [(0, 0)]
Row: 0 - Col: 1 - RowMax: 3 - ColMax: 3 - currVal: 9 - numPath: 1 - visited: [(0, 0)]
Row: 0 - Col: -1 - RowMax: 3 - ColMax: 3 - currVal: 9 - numPath: 1 - visited: [(0, 0)]
Row: 0 - Col: 1 - RowMax: 3 - ColMax: 3 - currVal: 9 - numPath: 0 - visited: [(0, 0)]
Row: 1 - Col: 1 - RowMax: 3 - ColMax: 3 - currVal: 9 - numPath: 1 - visited: [(0, 0), (0, 1)]
Row: -1 - Col: 1 - RowMax: 3 - ColMax: 3 - currVal: 9 - numPath: 1 - visited: [(0, 0), (0, 1)]
Row: 0 - Col: 2 - RowMax: 3 - ColMax: 3 - currVal: 9 - numPath: 1 - visited: [(0, 0), (0, 1)]
Row: 0 - Col: 0 - RowMax: 3 - ColMax: 3 - currVal: 9 - numPath: 1 - visited: [(0, 0), (0, 1)]
Row: 0 - Col: 2 - RowMax: 3 - ColMax: 3 - currVal: 4 - numPath: 0 - visited: [(0, 0), (0, 1)]
Row: 1 - Col: 2 - RowMax: 3 - ColMax: 3 - currVal: 4 - numPath: 1 - visited: [(0, 0), (0, 1), (0, 2)]
Row: 2 - Col: 2 - RowMax: 3 - ColMax: 3 - currVal: 8 - numPath: 2 - visited: [(0, 0), (0, 1), (0, 2), (1, 2)]
Row: 0 - Col: 2 - RowMax: 3 - ColMax: 3 - currVal: 8 - numPath: 2 - visited: [(0, 0), (0, 1), (0, 2), (1, 2)]
Row: 1 - Col: 3 - RowMax: 3 - ColMax: 3 - currVal: 8 - numPath: 2 - visited: [(0, 0), (0, 1), (0, 2), (1, 2)]
Row: 1 - Col: 1 - RowMax: 3 - ColMax: 3 - currVal: 8 - numPath: 2 - visited: [(0, 0), (0, 1), (0, 2), (1, 2)]
Row: -1 - Col: 2 - RowMax: 3 - ColMax: 3 - currVal: 4 - numPath: 1 - visited: [(0, 0), (0, 1), (0, 2), (1, 2)]
Row: 0 - Col: 3 - RowMax: 3 - ColMax: 3 - currVal: 4 - numPath: 1 - visited: [(0, 0), (0, 1), (0, 2), (1, 2)]
Row: 0 - Col: 1 - RowMax: 3 - ColMax: 3 - currVal: 4 - numPath: 1 - visited: [(0, 0), (0, 1), (0, 2), (1, 2)]
Row: 1 - Col: 0 - RowMax: 3 - ColMax: 3 - currVal: 6 - numPath: 0 - visited: [(0, 0), (0, 1), (0, 2), (1, 2)]
Row: 2 - Col: 0 - RowMax: 3 - ColMax: 3 - currVal: 6 - numPath: 1 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0)]
Row: 0 - Col: 0 - RowMax: 3 - ColMax: 3 - currVal: 6 - numPath: 1 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0)]
Row: 1 - Col: 1 - RowMax: 3 - ColMax: 3 - currVal: 6 - numPath: 1 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0)]
Row: 1 - Col: -1 - RowMax: 3 - ColMax: 3 - currVal: 6 - numPath: 1 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0)]
Row: 2 - Col: -1 - RowMax: 3 - ColMax: 3 - currVal: 8 - numPath: 2 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0), (1, -1)]
Row: 0 - Col: -1 - RowMax: 3 - ColMax: 3 - currVal: 8 - numPath: 2 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0), (1, -1)]
Row: 1 - Col: 0 - RowMax: 3 - ColMax: 3 - currVal: 8 - numPath: 2 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0), (1, -1)]
Row: 1 - Col: -2 - RowMax: 3 - ColMax: 3 - currVal: 8 - numPath: 2 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0), (1, -1)]
Row: 1 - Col: 1 - RowMax: 3 - ColMax: 3 - currVal: 6 - numPath: 0 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0), (1, -1)]
Row: 2 - Col: 1 - RowMax: 3 - ColMax: 3 - currVal: 6 - numPath: 1 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0), (1, -1), (1, 1)]
Row: 0 - Col: 1 - RowMax: 3 - ColMax: 3 - currVal: 6 - numPath: 1 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0), (1, -1), (1, 1)]
Row: 1 - Col: 2 - RowMax: 3 - ColMax: 3 - currVal: 6 - numPath: 1 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0), (1, -1), (1, 1)]
Row: 1 - Col: 0 - RowMax: 3 - ColMax: 3 - currVal: 6 - numPath: 1 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0), (1, -1), (1, 1)]
Row: 1 - Col: 2 - RowMax: 3 - ColMax: 3 - currVal: 8 - numPath: 0 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0), (1, -1), (1, 1)]
Row: 2 - Col: 0 - RowMax: 3 - ColMax: 3 - currVal: 2 - numPath: 0 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0), (1, -1), (1, 1)]
Row: 3 - Col: 0 - RowMax: 3 - ColMax: 3 - currVal: 2 - numPath: 1 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0), (1, -1), (1, 1), (2, 0)]
Row: 2 - Col: 1 - RowMax: 3 - ColMax: 3 - currVal: 2 - numPath: 1 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0), (1, -1), (1, 1), (2, 0)]
Row: 2 - Col: -1 - RowMax: 3 - ColMax: 3 - currVal: 2 - numPath: 1 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0), (1, -1), (1, 1), (2, 0)]
Row: 2 - Col: 1 - RowMax: 3 - ColMax: 3 - currVal: 1 - numPath: 0 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0), (1, -1), (1, 1), (2, 0)]
Row: 3 - Col: 1 - RowMax: 3 - ColMax: 3 - currVal: 1 - numPath: 1 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0), (1, -1), (1, 1), (2, 0), (2, 1)]
Row: 1 - Col: 1 - RowMax: 3 - ColMax: 3 - currVal: 1 - numPath: 1 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0), (1, -1), (1, 1), (2, 0), (2, 1)]
Row: 2 - Col: 2 - RowMax: 3 - ColMax: 3 - currVal: 1 - numPath: 1 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0), (1, -1), (1, 1), (2, 0), (2, 1)]
Row: 2 - Col: 0 - RowMax: 3 - ColMax: 3 - currVal: 1 - numPath: 1 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0), (1, -1), (1, 1), (2, 0), (2, 1)]
Row: 2 - Col: 2 - RowMax: 3 - ColMax: 3 - currVal: 1 - numPath: 0 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0), (1, -1), (1, 1), (2, 0), (2, 1)]
Row: 3 - Col: 2 - RowMax: 3 - ColMax: 3 - currVal: 1 - numPath: 1 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0), (1, -1), (1, 1), (2, 0), (2, 1), (2, 2)]
Row: 1 - Col: 2 - RowMax: 3 - ColMax: 3 - currVal: 1 - numPath: 1 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0), (1, -1), (1, 1), (2, 0), (2, 1), (2, 2)]
Row: 2 - Col: 3 - RowMax: 3 - ColMax: 3 - currVal: 1 - numPath: 1 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0), (1, -1), (1, 1), (2, 0), (2, 1), (2, 2)]
Row: 2 - Col: 1 - RowMax: 3 - ColMax: 3 - currVal: 1 - numPath: 1 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0), (1, -1), (1, 1), (2, 0), (2, 1), (2, 2)]
2
我的问题是什么?
解决方案
有两个主要问题: 每次在 main 中调用求解器时,访问的坐标数组都不会重置。查看您的调试输出,访问的数组只会随着每一行变大。
isValid 函数不验证坐标是否低于矩阵。您可以在此处看到 row 和 col 值两次变为负值:
Row: 1 - Col: -2 - RowMax: 3 - ColMax: 3 - currVal: 8 - numPath: 2 - visited: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 0), (1, -1)]
推荐阅读
- reactjs - 为什么我不能渲染自动完成?
- java - java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory
- javascript - 大使 Keyclock 设置 redurect_uri 与大使注销
- micronaut - Micronaut 中的上下文传播
- typescript - 打字稿:确保函数返回枚举的所有可能值?
- javascript - Python连续相等和不等运算符结果
- javascript - api端点依赖于另一个
- spring-boot - 将 SpringBoot 应用程序部署到远程 Tomcat 服务器
- robotframework - Generata jsondata 机器人框架
- swift - 如何在 SwiftUI“弹出”的 ScrollView 中制作卡片以居中