Skip to content

Commit 6341e3b

Browse files
authored
feat: add swift implementation to lcof problem: No.47 (doocs#2928)
1 parent a995ccc commit 6341e3b

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

lcof/面试题47. 礼物的最大价值/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,26 @@ public class Solution {
198198
}
199199
```
200200

201+
#### Swift
202+
203+
```swift
204+
class Solution {
205+
func maxValue(_ grid: [[Int]]) -> Int {
206+
let m = grid.count
207+
let n = grid[0].count
208+
var f = [[Int]](repeating: [Int](repeating: 0, count: n + 1), count: m + 1)
209+
210+
for i in 1...m {
211+
for j in 1...n {
212+
f[i][j] = max(f[i - 1][j], f[i][j - 1]) + grid[i - 1][j - 1]
213+
}
214+
}
215+
216+
return f[m][n]
217+
}
218+
}
219+
```
220+
201221
<!-- tabs:end -->
202222

203223
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
func maxValue(_ grid: [[Int]]) -> Int {
3+
let m = grid.count
4+
let n = grid[0].count
5+
var f = [[Int]](repeating: [Int](repeating: 0, count: n + 1), count: m + 1)
6+
7+
for i in 1...m {
8+
for j in 1...n {
9+
f[i][j] = max(f[i - 1][j], f[i][j - 1]) + grid[i - 1][j - 1]
10+
}
11+
}
12+
13+
return f[m][n]
14+
}
15+
}

0 commit comments

Comments
 (0)