Skip to content

Commit 1221106

Browse files
committed
房屋染色
1 parent 0c18ac8 commit 1221106

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

paint_house.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- coding: utf-8 -*-
2+
3+
class Solution:
4+
# @param {int[][]} costs n x 3 cost matrix
5+
# @return {int} an integer, the minimum cost to paint all houses
6+
def minCost(self, costs):
7+
# Write your code here
8+
# 计算出每一层选某个颜色的最小值
9+
if not costs:
10+
return 0
11+
curr_costs = costs[0]
12+
for cost in costs[1:]:
13+
tmp = []
14+
for i in xrange(3):
15+
'''
16+
这里充分利用了负数索引的性质
17+
i = 0: -1 = 2; -2 = 1
18+
i = 1: 0 = 0; -1 = 2
19+
i = 2: 1 = 1; 0 = 0
20+
'''
21+
tmp.append(cost[i] + min(curr_costs[i - 1], curr_costs[i - 2]))
22+
curr_costs = tmp
23+
return min(curr_costs)

0 commit comments

Comments
 (0)