We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 0c18ac8 commit 1221106Copy full SHA for 1221106
paint_house.py
@@ -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