Skip to content

Commit a05efd6

Browse files
author
Partho Biswas
committed
no message
1 parent 2a28db9 commit a05efd6

File tree

3 files changed

+66
-9
lines changed

3 files changed

+66
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
707707
|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 |
708708
|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 |
709709
|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. |
710-
|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 |
710+
|41| **[62. Unique Paths](https://tinyurl.com/j8xmh7u)** | [Python](https://tinyurl.com/wu6rdaw/62_Unique_Paths.py), [Swift](https://tinyurl.com/wuja3c4/62_Unique_Paths.swift)| **[Vid 1](https://tinyurl.com/wp6vsqq)**, [Art 1](https://tinyurl.com/roammlg), [Art 2](https://tinyurl.com/tt5fpjk) | Medium | TODO: Check again |
711711
|42| **[152. Maximum Product Subarray](https://tinyurl.com/rxbfx5k)** | [Python](https://tinyurl.com/wu6rdaw/152_Maximum_Product_Subarray.py), [Swift](https://tinyurl.com/wuja3c4/152_Maximum_Product_Subarray.swift)| **[Vid 1](https://tinyurl.com/t24rc22)**, [Art 1](https://tinyurl.com/uzdpw4k) | Medium | Kadane's algorithm on multiplication |
712712
|43| **[64. Minimum Path Sum](https://tinyurl.com/ugnoql2)** | [Python](https://tinyurl.com/wu6rdaw/64_Minimum_Path_Sum.py), [Swift](https://tinyurl.com/wuja3c4/64_Minimum_Path_Sum.swift)| --- | Medium | --- |
713713
|44| **[91. Decode Ways](https://tinyurl.com/t5yx86t)** | [Python](https://tinyurl.com/wu6rdaw/91_Decode_Ways.py), [Swift](https://tinyurl.com/wuja3c4/91_Decode_Ways.swift)| [Vid 1](https://tinyurl.com/wxc7jlj) | Medium | --- |
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1 @@
1-
import Foundation
2-
class Solution {
3-
func uniquePaths(_ m: Int, _ n: Int) -> Int {
4-
var totalUPaths = 0
5-
var grid = Array(repeating: Array(repeating: 0, count: n), count: m)
6-
var visitedSet
7-
}
8-
}
1+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import Foundation
2+
3+
// Straight forward DFS. Time limit exceeded
4+
// Time: O(2^(m*n))
5+
class Solution {
6+
func uniquePaths(_ m: Int, _ n: Int) -> Int {
7+
return uniquePathHelper((m, n), (0,0))
8+
}
9+
10+
func uniquePathHelper(_ destinationCoord: (Int, Int), _ currentCoord: (Int, Int)) -> Int {
11+
if currentCoord == (destinationCoord.0 - 1, destinationCoord.1 - 1) {
12+
return 1
13+
}
14+
if currentCoord.0 >= destinationCoord.0 || currentCoord.1 >= destinationCoord.1 {
15+
return 0
16+
}
17+
18+
let right = uniquePathHelper(destinationCoord, (currentCoord.0 + 1, currentCoord.1))
19+
let down = uniquePathHelper(destinationCoord, (currentCoord.0, currentCoord.1 + 1))
20+
return right + down
21+
}
22+
}
23+
24+
25+
// Top-Down with memoizations. Accepted
26+
// Time: O(2^(m*n))
27+
class Solution {
28+
func uniquePaths(_ m: Int, _ n: Int) -> Int {
29+
var memo = [String:Int]()
30+
return uniquePathHelper((m, n), (0,0), &memo)
31+
}
32+
33+
func uniquePathHelper(_ destinationCoord: (Int, Int), _ currentCoord: (Int, Int), _ memo: inout [String:Int]) -> Int {
34+
if let value = memo["\(currentCoord.0)-\(currentCoord.1)"] {
35+
return value
36+
}
37+
if currentCoord == (destinationCoord.0 - 1, destinationCoord.1 - 1) {
38+
return 1
39+
}
40+
if currentCoord.0 >= destinationCoord.0 || currentCoord.1 >= destinationCoord.1 {
41+
return 0
42+
}
43+
44+
let right = uniquePathHelper(destinationCoord, (currentCoord.0 + 1, currentCoord.1), &memo)
45+
let down = uniquePathHelper(destinationCoord, (currentCoord.0, currentCoord.1 + 1), &memo)
46+
memo["\(currentCoord.0)-\(currentCoord.1)"] = right + down
47+
return right + down
48+
}
49+
}
50+
51+
52+
// Bottom-Up dp. Accepted
53+
// Time: O(m*n)
54+
class Solution {
55+
func uniquePaths(_ m: Int, _ n: Int) -> Int {
56+
var dpTable = Array(repeating: Array(repeating: 1, count: m), count: n)
57+
for i in 1..<n {
58+
for j in 1..<m {
59+
dpTable[i][j] = dpTable[i - 1][j] + dpTable[i][j - 1]
60+
}
61+
}
62+
return dpTable[n - 1][m - 1]
63+
}
64+
}

0 commit comments

Comments
 (0)