Leetcode: Squirrel Simulation

Squirrel Simulation

Similar Problems:

There’s a tree, a squirrel, and several nuts. Positions are represented by the cells in a 2D grid. Your goal is to find the minimal distance for the squirrel to collect all the nuts and put them under the tree one by one. 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. The distance is represented by the number of moves.
Example 1:

Height : 5
Width : 7
Tree position : [2,2]
Squirrel : [4,4]
Nuts : [[3,0], [2,5]]
Output: 12


Leetcode: Squirrel Simulation


  1. All given positions won’t overlap.
  2. The squirrel can take at most one nut at one time.
  3. The given positions of nuts have no order.
  4. Height and width are positive integers. 3 <= height * width <= 10,000.
  5. The given positions contain at least one nut, only one tree and one squirrel.

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.

  • Solution: XXX

General Thinkings:

Key Observations:

Walk Through Testdata

// Blog link: https://code.dennyzhang.com/squirrel-simulation


Share It, If You Like It.

Leave a Reply

Your email address will not be published.