-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathleetcode_256.py
More file actions
37 lines (30 loc) · 1 KB
/
leetcode_256.py
File metadata and controls
37 lines (30 loc) · 1 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 22 13:19:10 2026
@author: rishigoswamy
Problem:
Each house can be painted Red, Green, or Blue.
No two adjacent houses can have the same color.
Return the minimum total cost.
Approach:
Dynamic Programming.
For each house:
cost[color] = current cost +
minimum of previous house's other two colors
Time Complexity: O(n)
Space Complexity: O(1)
"""
from typing import List
#https://leetcode.com/problems/paint-house/
class Solution:
def minCost(self, costs: List[List[int]]) -> int:
currCost = costs[0]
val = min(costs[0])
for i in range(1, len(costs)):
costR = costs[i][0] + min(currCost[1], currCost[2])
costG = costs[i][1] + min(currCost[0], currCost[2])
costB = costs[i][2] + min(currCost[1], currCost[0])
val = min(costR, costG, costB)
currCost = [costR, costG, costB]
return val