05. Walls Coloring II
β GFG solution to the Walls Coloring II problem: find minimum cost to paint walls with different colors for adjacent walls using optimized DP with two minimum tracking. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given n walls arranged in a row, and each wall must be painted using one of the k available colors. The cost of painting the i-th wall with the j-th color is given by costs[i][j].
Your task is to determine the minimum total cost required to paint all the walls such that no two adjacent walls share the same color. If it is impossible to paint the walls under this condition, return -1.
π Examples
Example 1
Input: n = 4, k = 3,
costs[][] = [[1, 5, 7],
[5, 8, 4],
[3, 2, 9],
[1, 2, 4]]
Output: 8
Explanation:
Paint wall 0 with color 0. Cost = 1
Paint wall 1 with color 2. Cost = 4
Paint wall 2 with color 1. Cost = 2
Paint wall 3 with color 0. Cost = 1
Total Cost = 1 + 4 + 2 + 1 = 8Example 2
Input: n = 5, k = 1,
costs[][] = [[5],
[4],
[9],
[2],
[1]]
Output: -1
Explanation: It is not possible to color all the walls under the given conditions
(only 1 color available but need different colors for adjacent walls).π Constraints
$0 \le n \le 10^3$
$0 \le k \le 10^3$
$1 \le \text{costs}[i][j] \le 10^5$
β
My Approach
The optimal solution uses Dynamic Programming with Two Minimum Tracking to efficiently find the minimum cost while ensuring adjacent walls have different colors:
Two Minimum Tracking DP
Edge Case Handling:
If there's only 1 color available (
k = 1) and more than 1 wall (n > 1), it's impossible to paint adjacent walls with different colors. Return -1.
Initialize Tracking Variables:
m1(minimum cost from previous wall)m2(second minimum cost from previous wall)idx(color index that gave minimum cost)Initially, set
m1 = 0, m2 = 0, idx = -1(no previous wall)
Process Each Wall:
For each wall
ifrom 0 to n-1:Initialize
nm1 = INT_MAX(new minimum),nm2 = INT_MAX(new second minimum),nidx = -1(new minimum index)For each color
jfrom 0 to k-1:Calculate cost:
c = costs[i][j] + (if same color as previous minimum, use m2, else use m1)Update
nm1,nm2, andnidxbased on calculated cost
Update tracking variables:
m1 = nm1, m2 = nm2, idx = nidx
Return Result:
After processing all walls,
m1contains the minimum cost to paint all walls
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(nΓk), as we iterate through all n walls and for each wall, we check all k colors. Each element is processed once with constant time operations.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space to store tracking variables (m1, m2, idx, nm1, nm2, nidx) regardless of input size.
π§βπ» Code (C++)
class Solution {
public:
int minCost(vector<vector<int>>& costs) {
int n = costs.size(), m = costs[0].size();
if (m == 1 && n > 1) return -1;
int m1 = 0, m2 = 0, idx = -1;
for (int i = 0; i < n; i++) {
int nm1 = INT_MAX, nm2 = INT_MAX, nidx = -1;
for (int j = 0; j < m; j++) {
int c = costs[i][j] + (j == idx ? m2 : m1);
if (c < nm1) { nm2 = nm1; nm1 = c; nidx = j; }
else if (c < nm2) nm2 = c;
}
m1 = nm1; m2 = nm2; idx = nidx;
}
return m1;
}
};β Code (Java)
class Solution {
int minCost(int[][] costs) {
int n = costs.length, m = costs[0].length;
if (m == 1 && n > 1) return -1;
int m1 = 0, m2 = 0, idx = -1;
for (int i = 0; i < n; i++) {
int nm1 = Integer.MAX_VALUE, nm2 = Integer.MAX_VALUE, nidx = -1;
for (int j = 0; j < m; j++) {
int c = costs[i][j] + (j == idx ? m2 : m1);
if (c < nm1) { nm2 = nm1; nm1 = c; nidx = j; }
else if (c < nm2) nm2 = c;
}
m1 = nm1; m2 = nm2; idx = nidx;
}
return m1;
}
}π Code (Python)
class Solution:
def minCost(self, costs):
n, m = len(costs), len(costs[0])
if m == 1 and n > 1: return -1
m1, m2, idx = 0, 0, -1
for i in range(n):
nm1, nm2, nidx = float('inf'), float('inf'), -1
for j in range(m):
c = costs[i][j] + (m2 if j == idx else m1)
if c < nm1: nm2, nm1, nidx = nm1, c, j
elif c < nm2: nm2 = c
m1, m2, idx = nm1, nm2, nidx
return m1π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated