Solving Leetcode Interviews in Seconds with AI: Paint House IV
Introduction
In this blog post, we will explore how to solve the LeetCode problem "3429" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
You are given an even integer n representing the number of houses arranged in a straight line, and a 2D array cost of size n x 3, where cost[i][j] represents the cost of painting house i with color j + 1. The houses will look beautiful if they satisfy the following conditions: No two adjacent houses are painted the same color. Houses equidistant from the ends of the row are not painted the same color. For example, if n = 6, houses at positions (0, 5), (1, 4), and (2, 3) are considered equidistant. Return the minimum cost to paint the houses such that they look beautiful. Example 1: Input: n = 4, cost = [[3,5,7],[6,2,9],[4,8,1],[7,3,5]] Output: 9 Explanation: The optimal painting sequence is [1, 2, 3, 2] with corresponding costs [3, 2, 1, 3]. This satisfies the following conditions: No adjacent houses have the same color. Houses at positions 0 and 3 (equidistant from the ends) are not painted the same color (1 != 2). Houses at positions 1 and 2 (equidistant from the ends) are not painted the same color (2 != 3). The minimum cost to paint the houses so that they look beautiful is 3 + 2 + 1 + 3 = 9. Example 2: Input: n = 6, cost = [[2,4,6],[5,3,8],[7,1,9],[4,6,2],[3,5,7],[8,2,4]] Output: 18 Explanation: The optimal painting sequence is [1, 3, 2, 3, 1, 2] with corresponding costs [2, 8, 1, 2, 3, 2]. This satisfies the following conditions: No adjacent houses have the same color. Houses at positions 0 and 5 (equidistant from the ends) are not painted the same color (1 != 2). Houses at positions 1 and 4 (equidistant from the ends) are not painted the same color (3 != 1). Houses at positions 2 and 3 (equidistant from the ends) are not painted the same color (2 != 3). The minimum cost to paint the houses so that they look beautiful is 2 + 8 + 1 + 2 + 3 + 2 = 18. Constraints: 2 <= n <= 105 n is even. cost.length == n cost[i].length == 3 0 <= cost[i][j] <= 105
Explanation
Here's the solution to the problem, addressing the constraints and efficiency requirements:
High-Level Approach:
- Dynamic Programming: Maintain a 2D DP table where
dp[i][j]stores the minimum cost to paint houses up to indexiwith houseipainted colorj. - Constraints Handling: During the DP update, ensure no adjacent houses have the same color and houses equidistant from the ends have different colors.
- Optimization: Since we only need the previous row of the DP table to compute the current row, use rolling array optimization to reduce space complexity.
- Dynamic Programming: Maintain a 2D DP table where
Complexity:
- Runtime: O(n)
- Storage: O(1)
Code
def min_cost_to_paint(n: int, costs: list[list[int]]) -> int:
"""
Calculates the minimum cost to paint houses such that no two adjacent houses
are painted the same color and houses equidistant from the ends are not
painted the same color.
Args:
n: The number of houses.
costs: A 2D array where costs[i][j] is the cost to paint house i with color j+1.
Returns:
The minimum cost to paint the houses.
"""
dp = costs[0] # Initialize with costs of the first house
for i in range(1, n):
new_dp = [0] * 3
for j in range(3):
min_cost = float('inf')
for k in range(3):
if k != j: # Ensure adjacent houses have different colors
if i != n - 1 - i or k != dp.index(min(dp)): #Equidistant Constraint
min_cost = min(min_cost, dp[k])
new_dp[j] = costs[i][j] + min_cost
dp = new_dp # Update dp to the current row
return min(dp)