Advertisement

Escape The Ghosts - LeetCode 789 Solution

Escape The Ghosts - Complete Solution Guide

Escape The Ghosts is LeetCode problem 789, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are playing a simplified PAC-MAN game on an infinite 2-D grid. You start at the point [0, 0] , and you are given a destination point target = [x target , y target ] that you are trying to get to. There are several ghosts on the map with their starting positions given as a 2D array ghosts , where ghosts[i] = [x i , y i ] represents the starting position of the i th ghost. All inputs are integral coordinates . Each turn, you and all the ghosts may independently choose to either move 1 unit in

Detailed Explanation

The problem presents a scenario where you are playing a simplified Pac-Man game on an infinite 2D grid. You start at (0, 0) and need to reach a target location (x_target, y_target). There are multiple ghosts with their own starting positions. Both you and the ghosts can move one unit in any of the four cardinal directions (north, east, south, west) or stay still in each turn, simultaneously. The goal is to determine if you can reach the target *before* any ghost reaches you. If a ghost and you arrive at the same square at the same time, it's considered a failure to escape.

Solution Approach

The solution calculates the Manhattan distance from the starting point (0,0) to the target for the player and the Manhattan distance from each ghost's starting position to the target. It then compares the player's distance to each ghost's distance. If any ghost can reach the target in a distance less than or equal to the player's distance, it returns `false` (escape impossible). Otherwise, it returns `true` (escape possible).

Step-by-Step Algorithm

  1. Step 1: Calculate the Manhattan distance from the player's starting point (0, 0) to the target (target[0], target[1]). This distance is `abs(target[0]) + abs(target[1])`.
  2. Step 2: Iterate through the list of ghosts.
  3. Step 3: For each ghost, calculate the Manhattan distance from its starting position (ghost[0], ghost[1]) to the target (target[0], target[1]). This distance is `abs(ghost[0] - target[0]) + abs(ghost[1] - target[1])`.
  4. Step 4: Compare the ghost's distance to the player's distance. If the ghost's distance is less than or equal to the player's distance, it means the ghost can reach the target at the same time or sooner than the player, making escape impossible. Return `false` in this case.
  5. Step 5: If the loop completes without finding any ghost that can reach the target as fast or faster than the player, it means the player can escape. Return `true`.

Key Insights

  • Insight 1: The Manhattan distance (sum of absolute differences of coordinates) is the shortest distance between two points in this grid.
  • Insight 2: To escape, you must reach the target before any ghost. This implies your Manhattan distance to the target must be strictly less than any ghost's Manhattan distance to the target.
  • Insight 3: If at least one ghost can reach the target in the same number of steps or fewer than you, there's no way to escape, regardless of the ghost's movements.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array, Math.

Companies

Asked at: Wix.