573. Squirrel Simulation
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the minimum total distance a squirrel needs to travel to collect all nuts and deposit them at a tree. The squirrel starts at a given position, can carry only one nut at a time, and moves on a grid (implying Manhattan distance). The crucial decision is choosing which nut to collect first, as the path for the first nut is different from all subsequent nuts.
Solution
Squirrel Simulation in Python
class Solution:
def minDistance(self, height: int, width: int, tree: List[int], squirrel: List[int], nuts: List[List[int]]) -> int:
def dist(p1, p2):
return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1])
total_dist_from_tree = 0
max_saving = -float('inf')
# The logic is that the total path is the sum of round trips for each nut from the tree,
# adjusted for the first nut which starts from the squirrel's position.
# Total distance = (sum of 2 * dist(tree, nut) for all nuts) - (max saving)
# The saving for a nut is dist(tree, nut) - dist(squirrel, nut).
# We want to maximize this saving by choosing the best first nut.
for nut in nuts:
dist_nut_tree = dist(nut, tree)
dist_nut_squirrel = dist(nut, squirrel)
# Add the round trip distance from the tree to this nut to the total.
total_dist_from_tree += dist_nut_tree * 2
# Calculate the saving if this nut is chosen first.
saving = dist_nut_tree - dist_nut_squirrel
# Keep track of the maximum possible saving.
if saving > max_saving:
max_saving = saving
# The minimal total distance is the base distance (all round trips from tree)
# minus the maximum saving we can get by choosing the best first nut.
return total_dist_from_tree - max_saving
Squirrel Simulation in Java
class Solution {
public int minDistance(int height, int width, int[] tree, int[] squirrel, int[][] nuts) {
long totalDistFromTree = 0;
int maxSaving = Integer.MIN_VALUE;
// The logic is that the total path is the sum of round trips for each nut from the tree,
// adjusted for the first nut which starts from the squirrel's position.
// Total distance = (sum of 2 * dist(tree, nut) for all nuts) - (max saving)
// The saving for a nut is dist(tree, nut) - dist(squirrel, nut).
// We want to maximize this saving by choosing the best first nut.
for (int[] nut : nuts) {
int distNutTree = Math.abs(nut[0] - tree[0]) + Math.abs(nut[1] - tree[1]);
int distNutSquirrel = Math.abs(nut[0] - squirrel[0]) + Math.abs(nut[1] - squirrel[1]);
// Add the round trip distance from the tree to this nut to the total.
totalDistFromTree += (long)distNutTree * 2;
// Calculate the saving if this nut is chosen first.
int saving = distNutTree - distNutSquirrel;
// Keep track of the maximum possible saving.
if (saving > maxSaving) {
maxSaving = saving;
}
}
// The minimal total distance is the base distance (all round trips from tree)
// minus the maximum saving we can get by choosing the best first nut.
return (int)(totalDistFromTree - maxSaving);
}
}
Squirrel Simulation in C++
class Solution {
public:
int minDistance(int height, int width, vector<int>& tree, vector<int>& squirrel, vector<vector<int>>& nuts) {
// Helper lambda to calculate Manhattan distance
auto dist = [](const std::vector<int>& p1, const std::vector<int>& p2) {
return std::abs(p1[0] - p2[0]) + std::abs(p1[1] - p2[1]);
};
long long total_dist_from_tree = 0;
int max_saving = std::numeric_limits<int>::min();
// The logic is that the total path is the sum of round trips for each nut from the tree,
// adjusted for the first nut which starts from the squirrel's position.
// Total distance = (sum of 2 * dist(tree, nut) for all nuts) - (max saving)
// The saving for a nut is dist(tree, nut) - dist(squirrel, nut).
// We want to maximize this saving by choosing the best first nut.
for (const auto& nut : nuts) {
int dist_nut_tree = dist(nut, tree);
int dist_nut_squirrel = dist(nut, squirrel);
// Add the round trip distance from the tree to this nut to the total.
total_dist_from_tree += (long long)dist_nut_tree * 2;
// Calculate the saving if this nut is chosen first.
int saving = dist_nut_tree - dist_nut_squirrel;
// Keep track of the maximum possible saving.
if (saving > max_saving) {
max_saving = saving;
}
}
// The minimal total distance is the base distance (all round trips from tree)
// minus the maximum saving we can get by choosing the best first nut.
return total_dist_from_tree - max_saving;
}
};
Squirrel Simulation in TypeScript
function minDistance(height: number, width: number, tree: number[], squirrel: number[], nuts: number[][]): number {
const dist = (p1: number[], p2: number[]): number => {
return Math.abs(p1[0] - p2[0]) + Math.abs(p1[1] - p2[1]);
};
let totalDistFromTree = 0;
let maxSaving = -Infinity;
// The logic is that the total path is the sum of round trips for each nut from the tree,
// adjusted for the first nut which starts from the squirrel's position.
// Total distance = (sum of 2 * dist(tree, nut) for all nuts) - (max saving)
// The saving for a nut is dist(tree, nut) - dist(squirrel, nut).
// We want to maximize this saving by choosing the best first nut.
for (const nut of nuts) {
const distNutTree = dist(nut, tree);
const distNutSquirrel = dist(nut, squirrel);
// Add the round trip distance from the tree to this nut to the total.
totalDistFromTree += distNutTree * 2;
// Calculate the saving if this nut is chosen first.
const saving = distNutTree - distNutSquirrel;
// Keep track of the maximum possible saving.
if (saving > maxSaving) {
maxSaving = saving;
}
}
// The minimal total distance is the base distance (all round trips from tree)
// minus the maximum saving we can get by choosing the best first nut.
return totalDistFromTree - maxSaving;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.