Skip to content

Commit 7edc63b

Browse files
author
Partho Biswas
committed
62. Unique Paths
1 parent 2b13a80 commit 7edc63b

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
588588
|38| **[801. Minimum Swaps To Make Sequences Increasing](https://tinyurl.com/rvtcyvb)** | [Python](https://tinyurl.com/wu6rdaw/801_Minimum_Swaps_To_Make_Sequences_Increasing.py)| [Art 1](https://tinyurl.com/tzx7wpv), [Art 2](https://tinyurl.com/ugybcmz) | Medium | TODO: Check again. Very analytical and tricky to come up with |
589589
|39| **[279. Perfect Squares](https://tinyurl.com/jcjc6kf)** | [Python](https://tinyurl.com/wu6rdaw/279_Perfect_Squares.py)| **[Must](https://tinyurl.com/raomm4y)**, [Vid 1](https://tinyurl.com/rxpaqe8), [Vis 2](https://tinyurl.com/uevqohx) | Medium | TODO: Check again. Very Important. Very analytical and tricky to come up with |
590590
|40| **[139. Word Break](https://tinyurl.com/yyyy9dqz)** | [Python](https://tinyurl.com/wu6rdaw/139_Word_Break.py)| **[Must](https://tinyurl.com/sdzmo32)**, [Vid 1](https://tinyurl.com/s4hvakw), [Vis 2](https://tinyurl.com/rjwjanc), [Vid 3](https://tinyurl.com/rdxmwwg) | Medium | TODO: Check again. Very Important. |
591+
|41| **[62. Unique Paths](https://tinyurl.com/j8xmh7u)** | [Python](https://tinyurl.com/wu6rdaw/62_Unique_Paths.py)| **[Vid 1](https://tinyurl.com/wp6vsqq)**, [Art 1](https://tinyurl.com/roammlg), [Art 2](https://tinyurl.com/tt5fpjk) | Medium | TODO: Check again |
591592

592593

593594
</p>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Approach 1: Recursion.
2+
class Solution(object):
3+
def uniquePaths(self, m, n):
4+
"""
5+
:type m: int
6+
:type n: int
7+
:rtype: int
8+
"""
9+
return self.uniquePathsHelper(m, 0, n, 0, 0)
10+
11+
12+
def uniquePathsHelper(self, m, currentM, n, currentN, paths):
13+
if (currentN, currentM) == (n - 1, m - 1):
14+
return paths + 1
15+
if currentM >= m or currentN >= n:
16+
return paths
17+
down = self.uniquePathsHelper(m, currentM, n, currentN + 1, paths)
18+
right = self.uniquePathsHelper(m, currentM + 1, n, currentN, paths)
19+
return down + right
20+
21+
22+
# Approach 2: Recursion with memoization. Source: https://tinyurl.com/tvolzor
23+
class Solution(object):
24+
def uniquePaths(self, m, n):
25+
"""
26+
:type m: int
27+
:type n: int
28+
:rtype: int
29+
"""
30+
memo = {}
31+
return self.uniquePathsHelper(m, n, memo)
32+
33+
def uniquePathsHelper(self, m, n, memo):
34+
if (m, n) in memo:
35+
return memo[(m, n)]
36+
elif m == 1 or n == 1:
37+
return 1
38+
up = self.uniquePathsHelper(m, n - 1, memo)
39+
left = self.uniquePathsHelper(m - 1, n, memo)
40+
memo[(m, n)] = up + left
41+
return memo[(m, n)]
42+
43+
44+
45+
46+
# Approach 3: DP. Source: https://tinyurl.com/tvolzor
47+
class Solution(object):
48+
def uniquePaths(self, m, n):
49+
"""
50+
:type m: int
51+
:type n: int
52+
:rtype: int
53+
"""
54+
if not m or not n:
55+
return 0
56+
dp = [[1 for _ in range(m)] for _ in range(n)]
57+
for i in range(1, n):
58+
for j in range(1, m):
59+
dp[i][j] = dp[i][j - 1] + dp[i - 1][j]
60+
return dp[-1][-1]

0 commit comments

Comments
 (0)