Skip to content

Conversation

@Siddhram
Copy link
Contributor

This PR introduces a complete and well-documented implementation of the Unique Paths problem in R using a top-down dynamic programming (recursion with memoization) approach.


Overview

The Unique Paths algorithm computes the number of distinct paths to reach the bottom-right corner of a grid from the top-left corner. Movement is restricted to right or down directions, and some cells may be blocked.
The algorithm uses recursion with memoization to avoid redundant computations and efficiently calculates the number of valid paths.


Features

  • Solves the Unique Paths problem using Top-Down Dynamic Programming (Memoization)
  • Handles obstacles by marking blocked cells
  • Efficient handling of large grids through cached computations
  • Graceful handling of edge cases, including fully blocked grids
  • Returns the total number of valid paths from start to finish
  • Demonstrates a classic grid-based DP problem

Complexity

Time complexity (TC): O(m · n) — each cell is computed at most once.
Space complexity (SC):

  • Top-down: O(m · n) for the DP table + O(m + n) recursion stack.
  • Bottom-up (iterative alternative): O(m · n) for DP table, reducible to O(n) with rolling array optimization.

Demonstration

Run the following examples in an R session:

unique_paths_dp(3, 7)$count
unique_paths_optimized(3, 7)
unique_paths_memo(3, 7)
unique_paths_with_obstacles(matrix(c(
  0,0,0,0,
  0,1,0,0,
  0,0,0,0
), nrow = 3, byrow = TRUE))


---

This implementation strengthens the **Dynamic Programming** module by adding a fundamental grid-based path-counting problem widely used in combinatorics, robotics, and pathfinding applications.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a comprehensive implementation of the Unique Paths algorithm to the dynamic programming module, providing multiple solution approaches for counting paths in a grid with movement restrictions.

  • Implementation of three distinct algorithms: tabulation DP, space-optimized DP, and top-down memoization
  • Addition of obstacle handling variant for blocked grid cells
  • Comprehensive testing and performance comparison functionality

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
dynamic_programming/unique_paths.r New implementation with four different approaches to solve the unique paths problem, including comprehensive documentation and test cases
DIRECTORY.md Updated to include the new unique paths algorithm entry and reformatted list structure from bullet points to dashes

- [1 Knapsack Problem](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/0/0/1_knapsack_problem.r)
- [1 Knapsack Problem](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/0/1_knapsack_problem.r)
- [Coin Change](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/coin_change.r)
- [Minimum Path Sum](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/minimum_path_sum.r)
Copy link

Copilot AI Oct 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This entry appears to reference a file that was not added in this PR. If this is a new algorithm being added, the corresponding implementation file should be included, or this entry should be removed if it was added in error.

Suggested change
- [Minimum Path Sum](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/minimum_path_sum.r)

Copilot uses AI. Check for mistakes.
* [Shortest.Common.Supersequence](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/shortest.common.supersequence.r)
* [Unique.Letters.Count](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/unique.letters.count.r)

- [Burrows](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/burrows.r)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change the formatting? The list is sometimes automatically generated, so you shouldn't change formatting manually

@siriak
Copy link
Member

siriak commented Oct 14, 2025

We do not add Leetcode problems

@siriak siriak closed this Oct 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants