Skip to content

Commit d176e42

Browse files
authored
feat: add swift implementation to lcof2 problem: No.091 (doocs#3477)
1 parent d3768f5 commit d176e42

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lcof2/剑指 Offer II 091. 粉刷房子/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,23 @@ impl Solution {
156156
}
157157
```
158158

159+
#### Swift
160+
161+
```swift
162+
class Solution {
163+
func minCost(_ costs: [[Int]]) -> Int {
164+
var r = 0, g = 0, b = 0
165+
for cost in costs {
166+
let _r = r, _g = g, _b = b
167+
r = min(_g, _b) + cost[0]
168+
g = min(_r, _b) + cost[1]
169+
b = min(_r, _g) + cost[2]
170+
}
171+
return min(r, min(g, b))
172+
}
173+
}
174+
```
175+
159176
<!-- tabs:end -->
160177

161178
<!-- solution:end -->
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
func minCost(_ costs: [[Int]]) -> Int {
3+
var r = 0, g = 0, b = 0
4+
for cost in costs {
5+
let _r = r, _g = g, _b = b
6+
r = min(_g, _b) + cost[0]
7+
g = min(_r, _b) + cost[1]
8+
b = min(_r, _g) + cost[2]
9+
}
10+
return min(r, min(g, b))
11+
}
12+
}

0 commit comments

Comments
 (0)