Skip to content

ORIGINAL CODING QUESTION

Longest rising skill trail

Every cell of a skill map holds a level. A trail moves between side-adjacent cells, and each step must go to a STRICTLY higher level. Return the number of cells on the longest possible trail; any single cell is a trail of length 1.

Open practice workspace

Original exercise · hard. Editorial role relevance does not imply a company has asked this question.

Input and output

rows cols, then rows×cols cell values in row-major order.

Constraints

1 ≤ rows,cols ≤ 8; 0 ≤ level ≤ 100.

Example 1

Input

3 3 1 2 3 6 5 4 7 8 9

Output

9

The levels snake upward 1 through 9 across all nine cells.

Example 2

Input

2 2 5 5 5 5

Output

1

Equal levels never allow a step.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

Strictly rising steps can never revisit a cell, so there are no cycles.

Hint 2

The best trail starting at a cell is 1 plus the best trail from a higher neighbour.

Hint 3

Memoise that value per cell, or process cells from highest level to lowest.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →