We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Squirrel Simulation

Number: 573

Difficulty: Medium

Paid? Yes

Companies: Block


Problem Description

Given the dimensions of a garden and the locations of a tree, a squirrel, and several nuts, find the minimal number of moves required for the squirrel to gather all nuts and drop each one off at the tree sequentially. The squirrel can only carry one nut at a time and moves in the four cardinal directions with each move increasing the distance by one.


Key Insights

  • Every round trip from the tree to a nut and back costs 2 * (Manhattan distance between nut and tree).
  • The squirrel’s first trip starts from its initial position rather than the tree, which provides an opportunity to save some distance.
  • The extra saving is calculated by comparing the distance from the squirrel to a nut versus the distance from the tree to that nut.
  • For each nut, the cost would be 2 * (distance from nut to tree), but the first nut the squirrel gathers saves: (distance from nut to tree) - (distance from squirrel to nut).
  • Maximizing this saving among all nuts minimizes the overall total moves.

Space and Time Complexity

Time Complexity: O(n), where n is the number of nuts, as we iterate over all nuts once. Space Complexity: O(1), since only constant extra space is used regardless of the input size.


Solution

The solution computes the total trip distance as if the squirrel collected every nut starting and finishing at the tree. For each nut, this is 2 * (Manhattan distance from that nut to the tree). However, for the first nut the squirrel collects, the trip starts from its initial position. The extra distance needed for the first trip is the Manhattan distance from the squirrel to that nut instead of from the tree to that nut. To minimize the total moves, we want to choose the nut that maximizes the distance saving, which is calculated as (distance(nut, tree) - distance(squirrel, nut)). The final result is the sum of all round-trip distances minus the maximum saving.

Data structures used: simple integer variables and iteration over the list of nuts. The algorithm is based on the Manhattan distance calculation and a greedy choice for the first nut.


Code Solutions

def min_distance(height, width, tree, squirrel, nuts):
    # Compute Manhattan distance between two points
    def manhattan(p1, p2):
        return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1])
    
    total_distance = 0
    max_saving = float('-inf')
    
    # Iterate for each nut
    for nut in nuts:
        # Distance from the nut to the tree (round trip cost half)
        distance_tree_nut = manhattan(nut, tree)
        # Add twice the distance because the nut is carried to the tree and squirrel returns for the next
        total_distance += 2 * distance_tree_nut
        
        # Calculate saving by picking up this nut first
        saving = distance_tree_nut - manhattan(squirrel, nut)
        max_saving = max(max_saving, saving)
    
    # The maximum saving is subtracted from the total distance
    return total_distance - max_saving

# Example usage
print(min_distance(5, 7, [2,2], [4,4], [[3,0],[2,5]]))
← Back to All Questions