Skip to content

Commit 7953aad

Browse files
committed
feat: add solutions to lc problem: No.0256
No.0256.Paint House
1 parent 07d4b84 commit 7953aad

File tree

4 files changed

+71
-21
lines changed

4 files changed

+71
-21
lines changed

solution/0200-0299/0256.Paint House/README.md

+28-7
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747

4848
<!-- 这里可写通用的实现逻辑 -->
4949

50+
**方法一:动态规划**
51+
52+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 表示房子的数量。
53+
5054
<!-- tabs:start -->
5155

5256
### **Python3**
@@ -56,13 +60,10 @@
5660
```python
5761
class Solution:
5862
def minCost(self, costs: List[List[int]]) -> int:
59-
r, g, b = 0, 0, 0
60-
for cost in costs:
61-
_r, _g, _b = r, g, b
62-
r = min(_g, _b) + cost[0]
63-
g = min(_r, _b) + cost[1]
64-
b = min(_r, _g) + cost[2]
65-
return min(r, g, b)
63+
a = b = c = 0
64+
for ca, cb, cc in costs:
65+
a, b, c = min(b, c) + ca, min(a, c) + cb, min(a, b) + cc
66+
return min(a, b, c)
6667
```
6768

6869
### **Java**
@@ -124,6 +125,26 @@ func min(x, y int) int {
124125
}
125126
```
126127

128+
### **JavaScript**
129+
130+
```js
131+
/**
132+
* @param {number[][]} costs
133+
* @return {number}
134+
*/
135+
var minCost = function (costs) {
136+
let [a, b, c] = [0, 0, 0];
137+
for (let [ca, cb, cc] of costs) {
138+
[a, b, c] = [
139+
Math.min(b, c) + ca,
140+
Math.min(a, c) + cb,
141+
Math.min(a, b) + cc,
142+
];
143+
}
144+
return Math.min(a, b, c);
145+
};
146+
```
147+
127148
### **...**
128149

129150
```

solution/0200-0299/0256.Paint House/README_EN.md

+24-7
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,10 @@ Minimum cost: 2 + 5 + 3 = 10.
5050
```python
5151
class Solution:
5252
def minCost(self, costs: List[List[int]]) -> int:
53-
r, g, b = 0, 0, 0
54-
for cost in costs:
55-
_r, _g, _b = r, g, b
56-
r = min(_g, _b) + cost[0]
57-
g = min(_r, _b) + cost[1]
58-
b = min(_r, _g) + cost[2]
59-
return min(r, g, b)
53+
a = b = c = 0
54+
for ca, cb, cc in costs:
55+
a, b, c = min(b, c) + ca, min(a, c) + cb, min(a, b) + cc
56+
return min(a, b, c)
6057
```
6158

6259
### **Java**
@@ -116,6 +113,26 @@ func min(x, y int) int {
116113
}
117114
```
118115

116+
### **JavaScript**
117+
118+
```js
119+
/**
120+
* @param {number[][]} costs
121+
* @return {number}
122+
*/
123+
var minCost = function (costs) {
124+
let [a, b, c] = [0, 0, 0];
125+
for (let [ca, cb, cc] of costs) {
126+
[a, b, c] = [
127+
Math.min(b, c) + ca,
128+
Math.min(a, c) + cb,
129+
Math.min(a, b) + cc,
130+
];
131+
}
132+
return Math.min(a, b, c);
133+
};
134+
```
135+
119136
### **...**
120137

121138
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number[][]} costs
3+
* @return {number}
4+
*/
5+
var minCost = function (costs) {
6+
let [a, b, c] = [0, 0, 0];
7+
for (let [ca, cb, cc] of costs) {
8+
[a, b, c] = [
9+
Math.min(b, c) + ca,
10+
Math.min(a, c) + cb,
11+
Math.min(a, b) + cc,
12+
];
13+
}
14+
return Math.min(a, b, c);
15+
};
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
class Solution:
22
def minCost(self, costs: List[List[int]]) -> int:
3-
r, g, b = 0, 0, 0
4-
for cost in costs:
5-
_r, _g, _b = r, g, b
6-
r = min(_g, _b) + cost[0]
7-
g = min(_r, _b) + cost[1]
8-
b = min(_r, _g) + cost[2]
9-
return min(r, g, b)
3+
a = b = c = 0
4+
for ca, cb, cc in costs:
5+
a, b, c = min(b, c) + ca, min(a, c) + cb, min(a, b) + cc
6+
return min(a, b, c)

0 commit comments

Comments
 (0)