Minimum steps to cut all trees in a grid in increasing height order

Quick Overview

Find the minimum number of steps to cut every tree in a grid in increasing height order, starting from the top-left cell and moving only through cells that are not blocked, or report that it cannot be done. It tests breadth-first search on a grid, repeated shortest paths and complexity analysis.

Minimum steps to cut all trees in a grid in increasing height order

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

A forest is given as an `m x n` grid of integers: - `0` is a blocked cell that cannot be entered. - `1` is an open cell you can walk on. - A value greater than `1` is a tree, and the value is its height. A tree cell can be walked through. You start at the top-left cell `(0, 0)` and must cut every tree in ascending order of height. Each move goes to one of the four neighboring cells (up, down, left or right) and counts as one step. Cutting a tree takes no extra steps and turns its cell into `1`. Return the minimum total number of steps needed to cut all the trees, or `-1` if it is impossible to cut them all. ### Function Signature ```python def cut_off_trees(forest: list[list[int]]) -> int: ``` ### Rules - `forest` is a list of rows, and every row has the same length. - Tree heights are distinct, so the cutting order is fixed. - If `forest[0][0]` is `0`, return `-1`. - Walking over a tree without cutting it is allowed. If the start cell holds the shortest tree, cutting it costs 0 steps; if it holds a taller tree, you come back to it later. - If the start cell is open and the grid has no trees, return `0`. ### Constraints - `1 <= len(forest) <= 50` - `1 <= len(forest[i]) <= 50` - `0 <= forest[i][j] <= 10^9` - All trees have unique heights. ### Examples ```text Input: forest = [[1,2,3],[0,0,4],[7,6,5]] Output: 6 ``` The trees in order are 2, 3, 4, 5, 6 and 7. Tree 2 is one step from `(0, 0)`, and each following tree is one step from the previous one, so the walk takes 6 steps. ```text Input: forest = [[1,2,3],[0,0,0],[7,6,5]] Output: -1 ``` The middle row is fully blocked. After cutting trees 2 and 3 there is no path to tree 5 in the bottom row. ```text Input: forest = [[3,5,2]] Output: 5 ``` The shortest tree, 2, is at `(0, 2)`: walk 2 steps, passing over the uncut tree 5. Walk 2 steps back to `(0, 0)` to cut 3, then 1 step to `(0, 1)` to cut 5, for a total of `2 + 2 + 1 = 5`.

Overview: Find the minimum number of steps to cut every tree in a grid in increasing height order, starting from the top-left cell and moving only through cells that are not blocked, or report that it cannot be done. It tests breadth-first search on a grid, repeated shortest paths and complexity analysis.

Amazon logo
Amazon
Nov 25, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

A forest is given as an m x n grid of integers:

  • 0 is a blocked cell that cannot be entered.
  • 1 is an open cell you can walk on.
  • A value greater than 1 is a tree, and the value is its height. A tree cell can be walked through.

You start at the top-left cell (0, 0) and must cut every tree in ascending order of height. Each move goes to one of the four neighboring cells (up, down, left or right) and counts as one step. Cutting a tree takes no extra steps and turns its cell into 1. Return the minimum total number of steps needed to cut all the trees, or -1 if it is impossible to cut them all.

Function Signature

def cut_off_trees(forest: list[list[int]]) -> int:

Rules

  • forest is a list of rows, and every row has the same length.
  • Tree heights are distinct, so the cutting order is fixed.
  • If forest[0][0] is 0 , return -1 .
  • Walking over a tree without cutting it is allowed. If the start cell holds the shortest tree, cutting it costs 0 steps; if it holds a taller tree, you come back to it later.
  • If the start cell is open and the grid has no trees, return 0 .

Constraints

  • 1 <= len(forest) <= 50
  • 1 <= len(forest[i]) <= 50
  • 0 <= forest[i][j] <= 10^9
  • All trees have unique heights.

Examples

Input: forest = [[1,2,3],[0,0,4],[7,6,5]]
Output: 6

The trees in order are 2, 3, 4, 5, 6 and 7. Tree 2 is one step from (0, 0), and each following tree is one step from the previous one, so the walk takes 6 steps.

Input: forest = [[1,2,3],[0,0,0],[7,6,5]]
Output: -1

The middle row is fully blocked. After cutting trees 2 and 3 there is no path to tree 5 in the bottom row.

Input: forest = [[3,5,2]]
Output: 5

The shortest tree, 2, is at (0, 2): walk 2 steps, passing over the uncut tree 5. Walk 2 steps back to (0, 0) to cut 3, then 1 step to (0, 1) to cut 5, for a total of 2 + 2 + 1 = 5.