Robert
Robert

Reputation: 123

Loop always starting at 0,0 - Trying to solve "Longest Increasing Path in a Matrix". What am i not seeing?

I have been struggling to solve this leetcode question here: https://leetcode.com/problems/longest-increasing-path-in-a-matrix/

My code here: https://pastebin.com/QBcXrbJa

The question is basically asking for the longest "path" given a matrix. I have added what I expected the code wants

We have an isValid function thats inside solver() that checks

If it checks out, numPath (the variable that counts the length of the path) gets increased, we add the node to visited, and we continue checking all nodes around it

However....

  1. I am getting an incorrect output (example, the matrix [[9,9,4],[6,6,8],[2,1,1]] outputs 2 instead of the expected output of 4)

  2. From my debug code, I noticed that the first 4 visited is always (0,0), even if we do not start at (0, 0)

For example, in the main function, we have

    for rowKey in range(0, matrixR):
        for colKey in range(0, matrixC):
            longest.append(
                solver(matrix, rowKey, colKey, matrixR, matrixC, matrix[rowKey][colKey])
            )

which is supposed to change where the code starts, but thats not reflected in the debug code

Debug Output

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

What is my problem?

Upvotes: 0

Views: 87

Answers (1)

DJLawton
DJLawton

Reputation: 46

There are 2 main problems: The array of visited coordinates are not reset each time the solver is called in the main. Looking at your debug output, the visited array only gets bigger with each line.

The isValid function does not verify if the coordinates are inferior to the matrix. You can see the row and col values dipping into the negative twice here:

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)]

Upvotes: 1

Related Questions