Advertisement

Unique Paths III - LeetCode 980 Solution

Unique Paths III - Complete Solution Guide

Unique Paths III is LeetCode problem 980, a Hard 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 given an m x n integer array grid where grid[i][j] could be: 1 representing the starting square. There is exactly one starting square. 2 representing the ending square. There is exactly one ending square. 0 representing empty squares we can walk over. -1 representing obstacles that we cannot walk over. Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once . Example 1: Input: grid = [[1,0,0,0],[0,0,0,0

Detailed Explanation

The problem asks us to find the number of unique paths from a starting cell (marked as 1) to an ending cell (marked as 2) in a grid. The grid also contains empty cells (marked as 0) and obstacles (marked as -1). The path must traverse every non-obstacle cell exactly once. We can only move in four directions: up, down, left, and right.

Solution Approach

The provided code employs a backtracking algorithm to explore all possible paths from the starting cell to the ending cell. It keeps track of visited cells by temporarily marking them as obstacles (-1). Once a path reaches the ending cell, it checks if all non-obstacle cells have been visited. If so, it increments the path count. After exploring each direction, the cell is marked back to its original value to allow other paths to explore it.

Step-by-Step Algorithm

  1. Step 1: Initialize the number of non-obstacle cells and find the starting cell's coordinates.
  2. Step 2: Define a recursive backtrack function with the current cell's coordinates and the number of visited cells as parameters.
  3. Step 3: Base Case 1: If the current cell is out of bounds or is an obstacle, return 0 (invalid path).
  4. Step 4: Base Case 2: If the current cell is the ending cell, check if the number of visited cells equals the total number of non-obstacle cells. If so, return 1 (valid path); otherwise, return 0 (invalid path).
  5. Step 5: Mark the current cell as visited (set to -1).
  6. Step 6: Recursively call the backtrack function for all four possible directions (up, down, left, right), incrementing the visited cells count.
  7. Step 7: Unmark the current cell (set back to its original value) to allow other paths to explore it.
  8. Step 8: Return the total number of valid paths found from the current cell.

Key Insights

  • Insight 1: Backtracking is a suitable approach since we need to explore all possible paths from the starting cell to the ending cell.
  • Insight 2: To ensure each non-obstacle cell is visited exactly once, we can mark visited cells during the backtracking process and revert the marking when backtracking.
  • Insight 3: The critical condition for a valid path is that we reach the ending cell after visiting all non-obstacle cells. We need to count the number of non-obstacle cells at the beginning and compare it with the number of visited cells when we reach the ending cell.

Complexity Analysis

Time Complexity: O(4^(m*n))

Space Complexity: O(m*n)

Topics

This problem involves: Array, Backtracking, Bit Manipulation, Matrix.

Companies

Asked at: Cruise, Pinterest.