algorithm - Given a matrix of ints, find the longest consecutive snake of incrementing by 1 numbers -
this question has answer here:
- find maximum length of path in grid 4 answers
basically, have this:
0 9 5 3' 4 1 5' 4' 5 7' 6' 9 2 8' 5 10
in case, longest snake 3 -> 4 -> 5 -> 6 -> 7 -> 8. put ' behind numbers in show visually.
you can go both horizontally , vertically. matrix can n x m, there isn't limit number of rows , columns.
what optimal way figure out?
i've thought starting @ position n/2 , m/2, recursively doing breadth-first search , keeping track of max interval can find. i'm not sure how best tackle it.
you create graph nodes matrix positions , vertices pointing number n n+1 neighbour.
once graph built, problem amounts finding 1 of longest paths in graph.
Comments
Post a Comment