-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem19.py
66 lines (52 loc) · 2.09 KB
/
problem19.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# This problem was asked by Facebook.
# A builder is looking to build a row of N houses that can be of K different colors. He has a goal of minimizing cost while ensuring that no two neighboring houses are of the same color.
# Given an N by K matrix where the nth row and kth column represents the cost to build the nth house with kth color, return the minimum cost which achieves this goal.
import basics
def cheapestCombination(prices):
number_of_houses = len(prices)
if(number_of_houses == 0):
print('Incorrect input..')
return
number_of_colors = len(prices[0])
if(number_of_colors == 0):
print('Incorrect input..')
return
#Initialize total cost matrix with 0s
total_cost_matrix = []
for i in range(number_of_houses):
colors = []
for j in range(number_of_colors):
colors.append(0)
total_cost_matrix.append(colors)
for i in range(number_of_houses):
if(i == 0):
for j in range(number_of_colors):
total_cost_matrix[i][j] = prices[i][j]
else:
for j in range(number_of_colors):
previous_values = list(total_cost_matrix[i-1])
print('previous values: ', previous_values)
previous_values.pop(j)
min_value = min(previous_values)
print('min prev value: ', min_value)
total_cost_matrix[i][j] = prices[i][j] + \
min_value
print('price: ',total_cost_matrix[i][j])
print(total_cost_matrix)
def main():
print('Number of houses: ')
number_of_houses = basics.getNumber()
print('Number of colors: ')
number_of_colors = basics.getNumber()
prices=[]
for h in range(number_of_houses):
print('House ',h,' prices: ')
colors = basics.getNumberList()
if(len(colors) != number_of_colors):
print('Input length not matching number of colors..')
return
prices.append(colors)
print(prices)
cheapestCombination(prices)
if __name__ == '__main__':
main()