Skip to content

Commit 9496493

Browse files
committed
feat: add solutions to lcof2 problem: No.091
剑指 Offer II 091. 粉刷房子
1 parent 129aa08 commit 9496493

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,40 @@ func min(x, y int) int {
126126
}
127127
```
128128

129+
### **TypeScript**
130+
131+
```ts
132+
function minCost(costs: number[][]): number {
133+
let [r, g, b] = [0, 0, 0];
134+
for (const [_r, _g, _b] of costs) {
135+
[r, g, b] = [
136+
_r + Math.min(g, b),
137+
_g + Math.min(r, b),
138+
_b + Math.min(r, g),
139+
];
140+
}
141+
return Math.min(r, g, b);
142+
}
143+
```
144+
145+
### **Rust**
146+
147+
```rust
148+
impl Solution {
149+
pub fn min_cost(costs: Vec<Vec<i32>>) -> i32 {
150+
let mut dp = [0, 0, 0];
151+
for cost in costs.iter() {
152+
dp = [
153+
cost[0] + dp[1].min(dp[2]),
154+
cost[1] + dp[0].min(dp[2]),
155+
cost[2] + dp[0].min(dp[1]),
156+
];
157+
}
158+
*dp.iter().min().unwrap()
159+
}
160+
}
161+
```
162+
129163
### **...**
130164

131165
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
impl Solution {
2+
pub fn min_cost(costs: Vec<Vec<i32>>) -> i32 {
3+
let mut dp = [0, 0, 0];
4+
for cost in costs.iter() {
5+
dp = [
6+
cost[0] + dp[1].min(dp[2]),
7+
cost[1] + dp[0].min(dp[2]),
8+
cost[2] + dp[0].min(dp[1]),
9+
];
10+
}
11+
*dp.iter().min().unwrap()
12+
}
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function minCost(costs: number[][]): number {
2+
let [r, g, b] = [0, 0, 0];
3+
for (const [_r, _g, _b] of costs) {
4+
[r, g, b] = [
5+
_r + Math.min(g, b),
6+
_g + Math.min(r, b),
7+
_b + Math.min(r, g),
8+
];
9+
}
10+
return Math.min(r, g, b);
11+
}

0 commit comments

Comments
 (0)