Minimum Partition Cost Split
Problem Description
You have a row of `n` houses, each painted with a cost given in `paintCost[i][c]`, the cost to paint house `i` with colour `c` (0-indexed, `k` colours total). No two **adjacent** houses may share the same colour.
Return the **minimum total painting cost** to paint all houses.
**Example 1:**
```
Input: paintCost = [[7, 3, 8], [5, 6, 2], [9, 1, 4]]
Output: 8
Explanation: Paint house 0 with colour 1 (cost 3), house 1 with colour 2 (cost 2), house 2 with colour 1 (cost 1). Total = 6. Wait: 3 + 2 + 1 = 6. Output: 6
```
**Example 1:**
```
Input: paintCost = [[7, 3, 8], [5, 6, 2], [9, 1, 4]]
Output: 6
Explanation: Colour sequence 1→2→1 costs 3 + 2 + 1 = 6.
```
**Example 2:**
```
Input: paintCost = [[4, 9], [3, 7], [8, 2], [6, 5]]
Output: 14
Explanation: Colour sequence 0→1→0→1 costs 4 + 7 + 8 + 5 = 24. Sequence 0→1→1 invalid. Sequence 0→1→0→1: 4+7+8+5=24. Best: 1→0→1→1 invalid. 0→1→0→0 invalid. 1→0→0 invalid. Try 1→0→1→0: 9+3+2+6=20. Try 0→1→0→1: 4+7+8+5=24. Try 1→0→0→1: 9+3+8+5=25 invalid (house2&3 different ok but house1→2 same colour 0). 1→0→1→0: 9+3+2+6=20. 0→1→0→1: 4+7+8+5=24. 0→1→1 invalid adjacent. Let me recalculate: paintCost=[[4,9],[3,7],[8,2],[6,5]]. dp[0]=[4,9]. dp[1][0]=3+9=12, dp[1][1]=7+4=11. dp[2][0]=8+11=19, dp[2][1]=2+12=14. dp[3][0]=6+14=20, dp[3][1]=5+19=24. min=20.
Actually output should be 20.
```
**Example 2:**
```
Input: paintCost = [[4, 9], [3, 7], [8, 2], [6, 5]]
Output: 20
Explanation: Optimal sequence is colour 0→1→1→0... recheck: dp[2][1]=min(dp[1][0])+paintCost[2][1]=12+2=14. dp[3][0]=min(dp[2][1])+paintCost[3][0]=14+6=20. So paint 0→0→1→0 with costs 4+3+2+6=15? Wait 0→0 is adjacent same. 0→1→1→0 adjacent same at index 1-2. Correct: dp gives 20.
```
Constraints
- 1 <= paintCost.length <= 200
- 2 <= paintCost[0].length <= 200
- 1 <= paintCost[i][c] <= 10^4
- All rows of paintCost have the same length k
Follow-up
What if the houses are arranged in a circle so that house 0 and house n-1 are also adjacent? How does your DP need to change?
Hints
Try the problem first. If you get stuck, you can reveal hints one at a time.
Solution
Leaderboard
No entries yet for python.
Be the first — submit an accepted solution.