Skip to content

Commit 6be9690

Browse files
committed
1 parent 230d578 commit 6be9690

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

l33tcode/paint-house.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"math"
5+
)
6+
7+
func minCost(costs [][]int) int {
8+
if len(costs) == 0 {
9+
return 0
10+
}
11+
12+
houses := len(costs)
13+
colors := len(costs[0])
14+
for pos := range make([]int, houses) {
15+
if pos != 0 {
16+
for colorCur := range make([]int, colors) {
17+
curCost := costs[pos][colorCur]
18+
costs[pos][colorCur] = math.MaxInt64
19+
for colorPrev := range make([]int, colors) {
20+
if colorCur != colorPrev {
21+
costs[pos][colorCur] = int(math.Min(float64(costs[pos][colorCur]), float64(curCost+costs[pos-1][colorPrev])))
22+
}
23+
}
24+
}
25+
}
26+
}
27+
28+
return int(math.Min(math.Min(float64(costs[houses-1][0]), float64(costs[houses-1][1])), float64(costs[houses-1][2])))
29+
}

0 commit comments

Comments
 (0)