Skip to content

Commit bbcbc50

Browse files
committed
feat: add solutions to lc problem: No.0265
No.0265.Paint House II
1 parent e304b36 commit bbcbc50

File tree

6 files changed

+278
-2
lines changed

6 files changed

+278
-2
lines changed

solution/0200-0299/0265.Paint House II/README.md

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,126 @@
5454

5555
<!-- 这里可写通用的实现逻辑 -->
5656

57+
**方法一:动态规划**
58+
59+
定义 $f[i][j]$ 表示粉刷前 $i$ 个房子,且最后一个房子被粉刷成第 $j$ 种颜色的最小花费。答案为 $\min_{0 \leq j < k} f[n][j]$。
60+
61+
对于 $f[i][j]$,可以从 $f[i - 1][j']$ 转移而来,其中 $j' \neq j$。因此,可以得到状态转移方程:
62+
63+
$$
64+
f[i][j] = \min_{0 \leq j' < k, j' \neq j} f[i - 1][j'] + costs[i - 1][j]
65+
$$
66+
67+
由于 $f[i][j]$ 只与 $f[i - 1][j']$ 有关,因此可以使用滚动数组优化空间复杂度。
68+
69+
时间复杂度 $O(n \times k^2)$,空间复杂度 $O(k)$。其中 $n$ 和 $k$ 分别为房子数量和颜色数量。
70+
5771
<!-- tabs:start -->
5872

5973
### **Python3**
6074

6175
<!-- 这里可写当前语言的特殊实现逻辑 -->
6276

6377
```python
64-
78+
class Solution:
79+
def minCostII(self, costs: List[List[int]]) -> int:
80+
n, k = len(costs), len(costs[0])
81+
f = costs[0][:]
82+
for i in range(1, n):
83+
g = costs[i][:]
84+
for j in range(k):
85+
t = min(f[h] for h in range(k) if h != j)
86+
g[j] += t
87+
f = g
88+
return min(f)
6589
```
6690

6791
### **Java**
6892

6993
<!-- 这里可写当前语言的特殊实现逻辑 -->
7094

7195
```java
96+
class Solution {
97+
public int minCostII(int[][] costs) {
98+
int n = costs.length, k = costs[0].length;
99+
int[] f = costs[0].clone();
100+
for (int i = 1; i < n; ++i) {
101+
int[] g = costs[i].clone();
102+
for (int j = 0; j < k; ++j) {
103+
int t = Integer.MAX_VALUE;
104+
for (int h = 0; h < k; ++h) {
105+
if (h != j) {
106+
t = Math.min(t, f[h]);
107+
}
108+
}
109+
g[j] += t;
110+
}
111+
f = g;
112+
}
113+
return Arrays.stream(f).min().getAsInt();
114+
}
115+
}
116+
```
117+
118+
### **C++**
119+
120+
```cpp
121+
class Solution {
122+
public:
123+
int minCostII(vector<vector<int>>& costs) {
124+
int n = costs.size(), k = costs[0].size();
125+
vector<int> f = costs[0];
126+
for (int i = 1; i < n; ++i) {
127+
vector<int> g = costs[i];
128+
for (int j = 0; j < k; ++j) {
129+
int t = INT_MAX;
130+
for (int h = 0; h < k; ++h) {
131+
if (h != j) {
132+
t = min(t, f[h]);
133+
}
134+
}
135+
g[j] += t;
136+
}
137+
f = move(g);
138+
}
139+
return *min_element(f.begin(), f.end());
140+
}
141+
};
142+
```
72143
144+
### **Go**
145+
146+
```go
147+
func minCostII(costs [][]int) (ans int) {
148+
n, k := len(costs), len(costs[0])
149+
f := cp(costs[0])
150+
for i := 1; i < n; i++ {
151+
g := cp(costs[i])
152+
for j := 0; j < k; j++ {
153+
t := math.MaxInt32
154+
for h := 0; h < k; h++ {
155+
if h != j && t > f[h] {
156+
t = f[h]
157+
}
158+
}
159+
g[j] += t
160+
}
161+
f = g
162+
}
163+
ans = f[0]
164+
for _, v := range f {
165+
if ans > v {
166+
ans = v
167+
}
168+
}
169+
return
170+
}
171+
172+
func cp(arr []int) []int {
173+
t := make([]int, len(arr))
174+
copy(t, arr)
175+
return t
176+
}
73177
```
74178

75179
### **...**

solution/0200-0299/0265.Paint House II/README_EN.md

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,103 @@ Or paint house 0 into color 2, paint house 1 into color 0. Minimum cost: 3 + 2 =
5353
### **Python3**
5454

5555
```python
56-
56+
class Solution:
57+
def minCostII(self, costs: List[List[int]]) -> int:
58+
n, k = len(costs), len(costs[0])
59+
f = costs[0][:]
60+
for i in range(1, n):
61+
g = costs[i][:]
62+
for j in range(k):
63+
t = min(f[h] for h in range(k) if h != j)
64+
g[j] += t
65+
f = g
66+
return min(f)
5767
```
5868

5969
### **Java**
6070

6171
```java
72+
class Solution {
73+
public int minCostII(int[][] costs) {
74+
int n = costs.length, k = costs[0].length;
75+
int[] f = costs[0].clone();
76+
for (int i = 1; i < n; ++i) {
77+
int[] g = costs[i].clone();
78+
for (int j = 0; j < k; ++j) {
79+
int t = Integer.MAX_VALUE;
80+
for (int h = 0; h < k; ++h) {
81+
if (h != j) {
82+
t = Math.min(t, f[h]);
83+
}
84+
}
85+
g[j] += t;
86+
}
87+
f = g;
88+
}
89+
return Arrays.stream(f).min().getAsInt();
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
int minCostII(vector<vector<int>>& costs) {
100+
int n = costs.size(), k = costs[0].size();
101+
vector<int> f = costs[0];
102+
for (int i = 1; i < n; ++i) {
103+
vector<int> g = costs[i];
104+
for (int j = 0; j < k; ++j) {
105+
int t = INT_MAX;
106+
for (int h = 0; h < k; ++h) {
107+
if (h != j) {
108+
t = min(t, f[h]);
109+
}
110+
}
111+
g[j] += t;
112+
}
113+
f = move(g);
114+
}
115+
return *min_element(f.begin(), f.end());
116+
}
117+
};
118+
```
62119
120+
### **Go**
121+
122+
```go
123+
func minCostII(costs [][]int) (ans int) {
124+
n, k := len(costs), len(costs[0])
125+
f := cp(costs[0])
126+
for i := 1; i < n; i++ {
127+
g := cp(costs[i])
128+
for j := 0; j < k; j++ {
129+
t := math.MaxInt32
130+
for h := 0; h < k; h++ {
131+
if h != j && t > f[h] {
132+
t = f[h]
133+
}
134+
}
135+
g[j] += t
136+
}
137+
f = g
138+
}
139+
ans = f[0]
140+
for _, v := range f {
141+
if ans > v {
142+
ans = v
143+
}
144+
}
145+
return
146+
}
147+
148+
func cp(arr []int) []int {
149+
t := make([]int, len(arr))
150+
copy(t, arr)
151+
return t
152+
}
63153
```
64154

65155
### **...**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int minCostII(vector<vector<int>>& costs) {
4+
int n = costs.size(), k = costs[0].size();
5+
vector<int> f = costs[0];
6+
for (int i = 1; i < n; ++i) {
7+
vector<int> g = costs[i];
8+
for (int j = 0; j < k; ++j) {
9+
int t = INT_MAX;
10+
for (int h = 0; h < k; ++h) {
11+
if (h != j) {
12+
t = min(t, f[h]);
13+
}
14+
}
15+
g[j] += t;
16+
}
17+
f = move(g);
18+
}
19+
return *min_element(f.begin(), f.end());
20+
}
21+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
func minCostII(costs [][]int) (ans int) {
2+
n, k := len(costs), len(costs[0])
3+
f := cp(costs[0])
4+
for i := 1; i < n; i++ {
5+
g := cp(costs[i])
6+
for j := 0; j < k; j++ {
7+
t := math.MaxInt32
8+
for h := 0; h < k; h++ {
9+
if h != j && t > f[h] {
10+
t = f[h]
11+
}
12+
}
13+
g[j] += t
14+
}
15+
f = g
16+
}
17+
ans = f[0]
18+
for _, v := range f {
19+
if ans > v {
20+
ans = v
21+
}
22+
}
23+
return
24+
}
25+
26+
func cp(arr []int) []int {
27+
t := make([]int, len(arr))
28+
copy(t, arr)
29+
return t
30+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int minCostII(int[][] costs) {
3+
int n = costs.length, k = costs[0].length;
4+
int[] f = costs[0].clone();
5+
for (int i = 1; i < n; ++i) {
6+
int[] g = costs[i].clone();
7+
for (int j = 0; j < k; ++j) {
8+
int t = Integer.MAX_VALUE;
9+
for (int h = 0; h < k; ++h) {
10+
if (h != j) {
11+
t = Math.min(t, f[h]);
12+
}
13+
}
14+
g[j] += t;
15+
}
16+
f = g;
17+
}
18+
return Arrays.stream(f).min().getAsInt();
19+
}
20+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def minCostII(self, costs: List[List[int]]) -> int:
3+
n, k = len(costs), len(costs[0])
4+
f = costs[0][:]
5+
for i in range(1, n):
6+
g = costs[i][:]
7+
for j in range(k):
8+
t = min(f[h] for h in range(k) if h != j)
9+
g[j] += t
10+
f = g
11+
return min(f)

0 commit comments

Comments
 (0)