Unique Paths in Grid
Count paths from top-left to bottom-right. Can only move right or down.
Visualization
Interactive visualization for Unique Paths in Grid
Unique Paths (Robot Grid)
• Time: O(m × n)
• Space: O(m × n) or O(n) optimized
• dp[i][j] = dp[i-1][j] + dp[i][j-1]
Interactive visualization with step-by-step execution
Implementation
1function uniquePaths(m: number, n: number): number {
2 const dp = Array(m).fill(0).map(() => Array(n).fill(1));
3
4 for (let i = 1; i < m; i++) {
5 for (let j = 1; j < n; j++) {
6 dp[i][j] = dp[i-1][j] + dp[i][j-1];
7 }
8 }
9 return dp[m-1][n-1];
10}
11
12function uniquePathsCombinatorics(m: number, n: number): number {
13 let result = 1;
14 for (let i = 1; i <= Math.min(m-1, n-1); i++) {
15 result = result * (m + n - 1 - i) / i;
16 }
17 return Math.round(result);
18}Deep Dive
Theoretical Foundation
dp[i][j] = dp[i-1][j] + dp[i][j-1]. Base: dp[0][j]=1, dp[i][0]=1. Combinatorics: C(m+n-2, m-1).
Complexity
Time
O(m×n)
O(m×n)
O(m×n)
Space
O(m×n) or O(n) optimized
Applications
Use Cases
Related Algorithms
0/1 Knapsack Problem
A classic optimization problem where you must select items with given weights and values to maximize total value without exceeding the knapsack's capacity. Each item can be taken only once (0 or 1). This is a fundamental problem in combinatorial optimization, resource allocation, and decision-making scenarios.
Longest Common Subsequence (LCS)
Finds the longest subsequence common to two sequences. A subsequence is a sequence that appears in the same relative order but not necessarily contiguously. This is fundamental in diff utilities, DNA sequence analysis, and version control systems like Git.
Edit Distance (Levenshtein Distance)
Edit Distance, also known as Levenshtein Distance, computes the minimum number of single-character edits (insertions, deletions, or substitutions) required to transform one string into another. Named after Soviet mathematician Vladimir Levenshtein who introduced it in 1965, this fundamental algorithm has applications in spell checking, DNA sequence analysis, natural language processing, and plagiarism detection.
Longest Increasing Subsequence (LIS)
Longest Increasing Subsequence (LIS) finds the length of the longest subsequence in an array where all elements are in strictly increasing order. Unlike a subarray, a subsequence doesn't need to be contiguous - elements can be selected from anywhere as long as their order is preserved. This classic DP problem has two solutions: O(n²) dynamic programming and O(n log n) binary search with patience sorting. Applications include stock price analysis, scheduling, Box Stacking Problem, and Building Bridges Problem.