Squirrel Simulation - Problem

You are given two integers height and width representing a garden of size height x width. You are also given:

  • An array tree where tree = [treer, treec] is the position of the tree in the garden
  • An array squirrel where squirrel = [squirrelr, squirrelc] is the position of the squirrel in the garden
  • An array nuts where nuts[i] = [nutir, nutic] is the position of the ith nut in the garden

The squirrel can only take at most one nut at one time and can move in four directions: up, down, left, and right, to the adjacent cell. Return the minimal distance for the squirrel to collect all the nuts and put them under the tree one by one.

The distance is the number of moves.

Input & Output

Example 1 — Basic Garden
$ Input: height = 5, width = 7, tree = [2,2], squirrel = [4,4], nuts = [[3,0], [2,5]]
Output: 12
💡 Note: Squirrel collects nut at [3,0] first (distance 5), brings to tree (distance 3), then goes to [2,5] (distance 3), and brings back (distance 3). Total: 5+3+3+3 = 14. But optimal is 12 by choosing [2,5] first.
Example 2 — Single Nut
$ Input: height = 1, width = 3, tree = [0,1], squirrel = [0,0], nuts = [[0,2]]
Output: 3
💡 Note: Squirrel at [0,0] goes to nut at [0,2] (distance 2), then to tree at [0,1] (distance 1). Total: 2+1 = 3.
Example 3 — Multiple Nuts Close Together
$ Input: height = 3, width = 3, tree = [1,1], squirrel = [0,0], nuts = [[0,1], [1,0], [2,1]]
Output: 6
💡 Note: Base distance is 2×(1+1+1) = 6. Best savings comes from choosing optimal first nut, but all have same savings of 0. Total remains 6.

Constraints

  • 1 ≤ height, width ≤ 100
  • tree.length == 2
  • squirrel.length == 2
  • 1 ≤ nuts.length ≤ 5000
  • nuts[i].length == 2
  • 0 ≤ treer, squirrelr, nutir ≤ height
  • 0 ≤ treec, squirrelc, nutic ≤ width

Visualization

Tap to expand
Squirrel Simulation INPUT TREE SQUIRREL N1 N2 height = 5, width = 7 tree = [2, 2] squirrel = [4, 4] nuts = [[3,0], [2,5]] Manhattan Distance: |x1-x2| + |y1-y2| Squirrel moves: up/down/left/right ALGORITHM STEPS 1 Calculate Base Distance Sum of 2 * dist(tree, each nut) 2*(|2-3|+|2-0|) + 2*(|2-2|+|2-5|) = 2*3 + 2*3 = 6 + 6 = 12 2 First Nut Optimization First trip: squirrel-->nut-->tree saves one tree-->nut distance 3 Calc Extra Cost Each Nut extra = dist(sq,nut) - dist(tree,nut) Nut1: (|4-3|+|4-0|)-(3) = 5-3 = 2 Nut2: (|4-2|+|4-5|)-(3) = 3-3 = 0 4 Pick Min Extra Cost Choose nut with min extra cost min(2, 0) = 0 (pick Nut2 first) Result = 12 + 0 = 12 FORMULA: totalDist = baseSum + minExtra = 12 + 0 = 12 FINAL RESULT Optimal Path SQ 1 2 3 3 3 3 Distance Breakdown: Sq-->Nut2-->Tree: 3 + 3 = 6 Tree-->Nut1-->Tree: 3 + 3 = 6 Total: 6 + 6 = 12 moves OUTPUT 12 Key Insight: The base cost is 2 * sum of all tree-to-nut distances (round trips). For the FIRST nut collected, we start from squirrel (not tree), so we add: dist(squirrel, nut) - dist(tree, nut) for that nut. Choose the first nut that MINIMIZES this extra cost for optimal solution. Time: O(n), Space: O(1) TutorialsPoint - Squirrel Simulation | Mathematical Optimization Approach
Asked in
Google 15 Amazon 8 Facebook 6
12.5K Views
Medium Frequency
~25 min Avg. Time
425 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen