From FAANG technical phone interview, maybe dfs, maybe need stack??
Anonymous User
82

So, the situation is that you have an infinite size matrix of n x m. Each cell has a value in it, could be negative or positive. There is a rook-like piece on the board that can move in any direction horizontally or vertically. The only restriction is that it can only move to a cell that has a higher number. The task is to find the longest possible path starting from any cell. Would seem to be depth first search problem looking for max path. But a catch, if that's what it is? Say you have a row -> 10 3 5 1 4 7 9. Start at 5. At first look, there's no path, as numbers of either side of 5 is smaller. But not so, 5 can go to 10, jumping over 3 for a path of 2. 5 can also jump over 1 and 4 to get to 7, then to 9, and because 10 is greater than 9, go all the way back over to 10 for a path of 10. If the row had 4 3 5 1 4 7 9, then it would have stopped at 9, unless there was a number in the column greater than 9, then it would jump however many cells to get to higher number along the column....
10 1 6 -5 13
12 9 2 4 -7
8 17 11 0 -12
15 3 -9 5 16
So, a matrix like this. Starting at 1, if you went to 13, you could then go to 16, then you're done. Or if you go to 10, then to 15, then to 16, now you're done and that's the longer path, if you start from 1. If you start for 10, you could got to 13, then 16. Or from 10 to 15 to 16. Either paths from 10 would less than starting from 1. Think I explained this right, anyone seen something like this before?

Comments (1)