Quick Overview

A squirrel on an obstacle-free grid must carry every nut to a tree, holding at most one nut at a time and moving one cell per step. The task asks for the minimum total number of moves over all delivery orders, testing reasoning about grid distances and how the order of deliveries affects the total.

Minimum Moves for a Squirrel to Carry Every Nut to a Tree One at a Time

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

A squirrel and a tree are on a rectangular grid with `height` rows and `width` columns, and several nuts lie on other cells of the grid. The squirrel must carry every nut to the tree. It can hold at most one nut at a time, and each move takes it to a cell that shares a side with its current cell. Return the minimum total number of moves the squirrel needs to deliver all the nuts to the tree. ### Function Signature ```python def min_collection_moves(height: int, width: int, tree: list[int], squirrel: list[int], nuts: list[list[int]]) -> int: ``` `tree`, `squirrel` and every `nuts[i]` are cells given as `[row, col]`. ### Rules - Row `0` is the top row and column `0` is the leftmost column. One move takes the squirrel from `(row, col)` to `(row - 1, col)`, `(row + 1, col)`, `(row, col - 1)` or `(row, col + 1)`. It never leaves the grid, and the grid has no obstacles. - The squirrel starts without a nut. Picking up a nut on its current cell and dropping a nut at the tree cost no moves. - The squirrel may walk over cells that hold other nuts without picking them up, and it never holds two nuts at once. - The nuts may be delivered in any order. Counting stops when the last nut is dropped at the tree; the squirrel does not need to go anywhere afterwards. - Return the minimum total number of moves over all delivery orders. ### Constraints - `1 <= height <= 100` and `1 <= width <= 100` - `0 <= row < height` and `0 <= col < width` for the tree, the squirrel and every nut. - `1 <= len(nuts) <= 5000` - The nuts lie on distinct cells, and no nut lies on the tree's cell. - The squirrel may start on the tree's cell or on a cell that holds a nut. - The answer is less than `2 * 10^6`. ### Examples **Example 1** ```text Input: height = 4, width = 5, tree = [1, 1], squirrel = [3, 4], nuts = [[0, 4], [3, 1]] Output: 11 ``` Fetching the nut at `(0, 4)` first takes 3 moves to reach it and 4 moves to carry it to the tree. The nut at `(3, 1)` then takes 2 moves out and 2 back, for 11 in total. Fetching `(3, 1)` first costs 3 + 2 + 4 + 4 = 13. **Example 2** ```text Input: height = 1, width = 10, tree = [0, 2], squirrel = [0, 9], nuts = [[0, 0], [0, 4]] Output: 11 ``` Fetching `(0, 4)` first costs 5 + 2, and `(0, 0)` then costs 2 + 2, for 11. Fetching `(0, 0)` first costs 9 + 2 + 2 + 2 = 15. **Example 3** ```text Input: height = 3, width = 3, tree = [1, 1], squirrel = [0, 0], nuts = [[0, 0], [2, 2], [0, 2]] Output: 10 ``` The squirrel picks up the nut on its own cell and carries it 2 moves to the tree. Each of the other two nuts takes 2 moves out and 2 back, for 2 + 4 + 4 = 10.

Overview: A squirrel on an obstacle-free grid must carry every nut to a tree, holding at most one nut at a time and moving one cell per step. The task asks for the minimum total number of moves over all delivery orders, testing reasoning about grid distances and how the order of deliveries affects the total.

A squirrel and a tree are on a rectangular grid with `height` rows and `width` columns, and one or more nuts lie on cells of the grid other than the tree's cell. The squirrel must carry every nut to the tree. It can hold at most one nut at a time, and each move takes it to a cell that shares a side with its current cell. Return the minimum total number of moves the squirrel needs to deliver all the nuts to the tree. `tree`, `squirrel` and every `nuts[i]` are cells given as `[row, col]`. ### Rules - Row `0` is the top row and column `0` is the leftmost column. One move takes the squirrel from `(row, col)` to `(row - 1, col)`, `(row + 1, col)`, `(row, col - 1)` or `(row, col + 1)`. It never leaves the grid, and the grid has no obstacles. - The squirrel starts without a nut. Picking up a nut on its current cell and dropping a nut at the tree cost no moves. - The squirrel may walk over cells that hold other nuts without picking them up, and it never holds two nuts at once. - The nuts may be delivered in any order. Counting stops when the last nut is dropped at the tree; the squirrel does not need to go anywhere afterwards. - Return the minimum total number of moves over all delivery orders, as a single integer. ### Constraints - `1 <= height <= 100` and `1 <= width <= 100` - `0 <= row < height` and `0 <= col < width` for the tree, the squirrel and every nut. - `1 <= len(nuts) <= 5000` - The nuts lie on distinct cells, and no nut lies on the tree's cell. - The squirrel may start on the tree's cell or on a cell that holds a nut. - The answer is less than `2 * 10^6`, so no value exceeds `2^31 - 1`; a 32-bit `int` holds the answer in Java and C++. ### Examples **Example 1** ```text Input: height = 4, width = 5, tree = [1, 1], squirrel = [3, 4], nuts = [[0, 4], [3, 1]] Output: 11 ``` Fetching the nut at `(0, 4)` first takes 3 moves to reach it and 4 moves to carry it to the tree. The nut at `(3, 1)` then takes 2 moves out and 2 back, for 11 in total. Fetching `(3, 1)` first costs 3 + 2 + 4 + 4 = 13. **Example 2** ```text Input: height = 1, width = 10, tree = [0, 2], squirrel = [0, 9], nuts = [[0, 0], [0, 4]] Output: 11 ``` Fetching `(0, 4)` first costs 5 + 2, and `(0, 0)` then costs 2 + 2, for 11. Fetching `(0, 0)` first costs 9 + 2 + 2 + 2 = 15.

Constraints

  • 1 <= height <= 100 and 1 <= width <= 100
  • 0 <= row < height and 0 <= col < width for the tree, the squirrel and every nut.
  • 1 <= len(nuts) <= 5000
  • The nuts lie on distinct cells, and no nut lies on the tree's cell.
  • The squirrel may start on the tree's cell or on a cell that holds a nut.
  • The answer is less than 2 * 10^6 (it never exceeds 2^31 - 1).

Examples

Input: (4, 5, [1, 1], [3, 4], [[0, 4], [3, 1]])

Expected Output: 11

Explanation: Source Example 1: fetch (0, 4) first for 3 + 4, then (3, 1) costs 2 + 2; total 11.

Input: (1, 10, [0, 2], [0, 9], [[0, 0], [0, 4]])

Expected Output: 11

Explanation: Source Example 2, one-row grid: every first-nut adjustment is positive (7 and 3), yet one nut must still be fetched from the start; 8 + 3 = 11.

Hints

  1. On a grid with no obstacles, how many moves does the shortest walk between two cells take?
  2. After the squirrel drops its first nut, where does every later trip begin and end?
  3. Ask which part of the total actually changes when you change the delivery order.

Loading coding console...

Show the approach

Approach

On a grid with no obstacles, the fewest moves between cells (r1, c1) and (r2, c2) is the Manhattan distance |r1 - r2| + |c1 - c2|, and a walk of that length always exists inside the grid. The squirrel carries one nut at a time and can drop it only at the tree, so the delivery is a sequence of trips: each trip walks from the squirrel's current cell to an undelivered nut and then carries it to the tree. After the first drop the squirrel is at the tree, so every later nut n costs exactly 2 * dist(n, tree) no matter the order. Only the first trip is different: it starts at the squirrel's cell and costs dist(squirrel, f) + dist(f, tree) for the chosen first nut f. Hence the cost of any order with first nut f is 2 * S + (dist(squirrel, f) - dist(f, tree)), where S is the sum of dist(n, tree) over all nuts, and every such cost is achievable. The minimum is therefore 2 * S plus the smallest adjustment dist(squirrel, f) - dist(f, tree) over all nuts. One pass accumulates S and the minimum adjustment. Invariant: after processing k nuts, total holds 2 * (sum of their tree distances) and best holds the smallest adjustment among them. Edge cases: when the squirrel starts on the tree every adjustment is 0 and the answer is 2 * S; when it starts on a nut, that nut's adjustment is -dist(nut, tree), which by the triangle inequality is the smallest possible value -dist(squirrel, tree); the adjustment can be positive for every nut, and it must still be added because one nut has to be fetched from the starting cell, so the minimum must not be clamped at 0; with a single nut the answer is dist(squirrel, nut) + dist(nut, tree). Ties among best first nuts give the same total, so the returned value is unique. The answer stays below 2 * 10^6, which fits a 32-bit int.

Time complexity:
O(n), where n = len(nuts)
Space complexity:
O(1)