|
| 1 | +--- |
| 2 | +id: paint-house |
| 3 | +title: Paint House |
| 4 | +sidebar_label: 0256. Paint House |
| 5 | +tags: [Hash Map, Two Pointers] |
| 6 | +description: Solution to finding the paint House. |
| 7 | +--- |
| 8 | + |
| 9 | +## Description |
| 10 | +- There is a row of n houses, where each house can be painted one of three colors: red, blue, or green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color. |
| 11 | + |
| 12 | +- The cost of painting each house with a certain color is represented by an n x 3 cost matrix costs. |
| 13 | + |
| 14 | +- For example, costs[0][0] is the cost of painting house 0 with the color red; costs[1][2] is the cost of painting house 1 with color green, and so on... |
| 15 | +Return the minimum cost to paint all houses. |
| 16 | + |
| 17 | +## Example: |
| 18 | + |
| 19 | +**Example 1:** |
| 20 | +```bash |
| 21 | +Input: costs = [[17,2,17],[16,16,5],[14,3,19]] |
| 22 | +Output: 10 |
| 23 | +Explanation: Paint house 0 into blue, paint house 1 into green, paint house 2 into blue. |
| 24 | +Minimum cost: 2 + 5 + 3 = 10. |
| 25 | +``` |
| 26 | + |
| 27 | +**Example 2:** |
| 28 | +```bash |
| 29 | +Input: costs = [[7,6,2]] |
| 30 | +Output: 2 |
| 31 | + ``` |
| 32 | + |
| 33 | +## Constraints: |
| 34 | +```bash |
| 35 | +costs.length == n |
| 36 | +costs[i].length == 3 |
| 37 | +1 <= n <= 100 |
| 38 | +1 <= costs[i][j] <= 20 |
| 39 | +``` |
| 40 | + |
| 41 | +## Solution |
| 42 | + |
| 43 | +#### Approach |
| 44 | + |
| 45 | +To solve the "Paint House" problem efficiently, we can use dynamic programming to keep track of the minimum cost to paint each house while ensuring no two adjacent houses have the same color. Here's the step-by-step approach to implement this solution: |
| 46 | + |
| 47 | +### Approach: |
| 48 | + |
| 49 | +1. **Initialization**: |
| 50 | + - Use the given cost matrix to keep track of the costs. Initialize variables to store the costs of painting the previous house red, blue, or green. |
| 51 | + |
| 52 | +2. **Dynamic Programming**: |
| 53 | + - Iterate through each house starting from the second one, updating the minimum cost to paint the current house with each color based on the previous house's costs. |
| 54 | + - For each house, calculate the cost to paint it red, blue, or green as the current cost plus the minimum of the other two costs from the previous house. |
| 55 | + |
| 56 | +3. **Result**: |
| 57 | + - The answer will be the minimum cost to paint the last house with any of the three colors. |
| 58 | + |
| 59 | +<Tabs> |
| 60 | +<TabItem value="javascript" label="JavaScript" default> |
| 61 | + <SolutionAuthor name="@sivaprasath"/> |
| 62 | + |
| 63 | + |
| 64 | +```javascript |
| 65 | +function minCost(costs) { |
| 66 | + if (costs.length === 0) return 0; |
| 67 | + |
| 68 | + let n = costs.length; |
| 69 | + |
| 70 | + // Initial cost for the first house |
| 71 | + let previousRed = costs[0][0]; |
| 72 | + let previousBlue = costs[0][1]; |
| 73 | + let previousGreen = costs[0][2]; |
| 74 | + |
| 75 | + for (let i = 1; i < n; i++) { |
| 76 | + let currentRed = costs[i][0] + Math.min(previousBlue, previousGreen); |
| 77 | + let currentBlue = costs[i][1] + Math.min(previousRed, previousGreen); |
| 78 | + let currentGreen = costs[i][2] + Math.min(previousRed, previousBlue); |
| 79 | + |
| 80 | + // Update previous costs to current costs for the next iteration |
| 81 | + previousRed = currentRed; |
| 82 | + previousBlue = currentBlue; |
| 83 | + previousGreen = currentGreen; |
| 84 | + } |
| 85 | + |
| 86 | + // The minimum cost of the last house can be any color |
| 87 | + return Math.min(previousRed, previousBlue, previousGreen); |
| 88 | +} |
| 89 | + |
| 90 | +// Example usage: |
| 91 | +const costs1 = [[17, 2, 17], [16, 16, 5], [14, 3, 19]]; |
| 92 | +console.log(minCost(costs1)); // Output: 10 |
| 93 | + |
| 94 | +const costs2 = [[7, 6, 2]]; |
| 95 | +console.log(minCost(costs2)); // Output: 2 |
| 96 | +``` |
| 97 | +</TabItem> |
| 98 | +<TabItem value="typescript" label="TypeScript"> |
| 99 | + <SolutionAuthor name="@sivaprasath"/> |
| 100 | + |
| 101 | +```typescript |
| 102 | +function minCost(costs: number[][]): number { |
| 103 | + if (costs.length === 0) return 0; |
| 104 | + |
| 105 | + let n = costs.length; |
| 106 | + |
| 107 | + // Initial cost for the first house |
| 108 | + let previousRed = costs[0][0]; |
| 109 | + let previousBlue = costs[0][1]; |
| 110 | + let previousGreen = costs[0][2]; |
| 111 | + |
| 112 | + for (let i = 1; i < n; i++) { |
| 113 | + let currentRed = costs[i][0] + Math.min(previousBlue, previousGreen); |
| 114 | + let currentBlue = costs[i][1] + Math.min(previousRed, previousGreen); |
| 115 | + let currentGreen = costs[i][2] + Math.min(previousRed, previousBlue); |
| 116 | + |
| 117 | + // Update previous costs to current costs for the next iteration |
| 118 | + previousRed = currentRed; |
| 119 | + previousBlue = currentBlue; |
| 120 | + previousGreen = currentGreen; |
| 121 | + } |
| 122 | + |
| 123 | + // The minimum cost of the last house can be any color |
| 124 | + return Math.min(previousRed, previousBlue, previousGreen); |
| 125 | +} |
| 126 | + |
| 127 | +// Example usage: |
| 128 | +const costs1: number[][] = [[17, 2, 17], [16, 16, 5], [14, 3, 19]]; |
| 129 | +console.log(minCost(costs1)); // Output: 10 |
| 130 | + |
| 131 | +const costs2: number[][] = [[7, 6, 2]]; |
| 132 | +console.log(minCost(costs2)); // Output: 2 |
| 133 | +``` |
| 134 | +</TabItem> |
| 135 | +<TabItem value="python" label="Python"> |
| 136 | + <SolutionAuthor name="@sivaprasath"/> |
| 137 | + |
| 138 | + |
| 139 | +```python |
| 140 | +def minCost(costs): |
| 141 | + if not costs: |
| 142 | + return 0 |
| 143 | + |
| 144 | + n = len(costs) |
| 145 | + |
| 146 | + # Initial cost for the first house |
| 147 | + previousRed = costs[0][0] |
| 148 | + previousBlue = costs[0][1] |
| 149 | + previousGreen = costs[0][2] |
| 150 | + |
| 151 | + for i in range(1, n): |
| 152 | + currentRed = costs[i][0] + min(previousBlue, previousGreen) |
| 153 | + currentBlue = costs[i][1] + min(previousRed, previousGreen) |
| 154 | + currentGreen = costs[i][2] + min(previousRed, previousBlue) |
| 155 | + |
| 156 | + # Update previous costs to current costs for the next iteration |
| 157 | + previousRed = currentRed |
| 158 | + previousBlue = currentBlue |
| 159 | + previousGreen = currentGreen |
| 160 | + |
| 161 | + # The minimum cost of the last house can be any color |
| 162 | + return min(previousRed, previousBlue, previousGreen) |
| 163 | + |
| 164 | +# Example usage: |
| 165 | +costs1 = [[17, 2, 17], [16, 16, 5], [14, 3, 19]] |
| 166 | +print(minCost(costs1)) # Output: 10 |
| 167 | + |
| 168 | +costs2 = [[7, 6, 2]] |
| 169 | +print(minCost(costs2)) # Output: 2 |
| 170 | +``` |
| 171 | + |
| 172 | +</TabItem> |
| 173 | + |
| 174 | +<TabItem value="java" label="Java"> |
| 175 | + <SolutionAuthor name="@sivaprasath"/> |
| 176 | + |
| 177 | + |
| 178 | +```java |
| 179 | +public class Solution { |
| 180 | + public int minCost(int[][] costs) { |
| 181 | + if (costs == null || costs.length == 0) { |
| 182 | + return 0; |
| 183 | + } |
| 184 | + |
| 185 | + int n = costs.length; |
| 186 | + |
| 187 | + // Initial cost for the first house |
| 188 | + int previousRed = costs[0][0]; |
| 189 | + int previousBlue = costs[0][1]; |
| 190 | + int previousGreen = costs[0][2]; |
| 191 | + |
| 192 | + for (int i = 1; i < n; i++) { |
| 193 | + int currentRed = costs[i][0] + Math.min(previousBlue, previousGreen); |
| 194 | + int currentBlue = costs[i][1] + Math.min(previousRed, previousGreen); |
| 195 | + int currentGreen = costs[i][2] + Math.min(previousRed, previousBlue); |
| 196 | + |
| 197 | + // Update previous costs to current costs for the next iteration |
| 198 | + previousRed = currentRed; |
| 199 | + previousBlue = currentBlue; |
| 200 | + previousGreen = currentGreen; |
| 201 | + } |
| 202 | + |
| 203 | + // The minimum cost of the last house can be any color |
| 204 | + return Math.min(previousRed, Math.min(previousBlue, previousGreen)); |
| 205 | + } |
| 206 | + |
| 207 | + public static void main(String[] args) { |
| 208 | + Solution solution = new Solution(); |
| 209 | + |
| 210 | + int[][] costs1 = {{17, 2, 17}, {16, 16, 5}, {14, 3, 19}}; |
| 211 | + System.out.println(solution.minCost(costs1)); // Output: 10 |
| 212 | + |
| 213 | + int[][] costs2 = {{7, 6, 2}}; |
| 214 | + System.out.println(solution.minCost(costs2)); // Output: 2 |
| 215 | + } |
| 216 | +} |
| 217 | +``` |
| 218 | + |
| 219 | +</TabItem> |
| 220 | +<TabItem value="cpp" label="C++"> |
| 221 | + <SolutionAuthor name="@sivaprasath"/> |
| 222 | + |
| 223 | +```cpp |
| 224 | +#include <iostream> |
| 225 | +#include <vector> |
| 226 | +#include <algorithm> |
| 227 | +#include <climits> |
| 228 | + |
| 229 | +using namespace std; |
| 230 | + |
| 231 | +class Solution { |
| 232 | +public: |
| 233 | + int minCost(vector<vector<int>>& costs) { |
| 234 | + if (costs.empty()) return 0; |
| 235 | + |
| 236 | + int n = costs.size(); |
| 237 | + |
| 238 | + // Initial cost for the first house |
| 239 | + int previousRed = costs[0][0]; |
| 240 | + int previousBlue = costs[0][1]; |
| 241 | + int previousGreen = costs[0][2]; |
| 242 | + |
| 243 | + for (int i = 1; i < n; ++i) { |
| 244 | + int currentRed = costs[i][0] + min(previousBlue, previousGreen); |
| 245 | + int currentBlue = costs[i][1] + min(previousRed, previousGreen); |
| 246 | + int currentGreen = costs[i][2] + min(previousRed, previousBlue); |
| 247 | + |
| 248 | + // Update previous costs to current costs for the next iteration |
| 249 | + previousRed = currentRed; |
| 250 | + previousBlue = currentBlue; |
| 251 | + previousGreen = currentGreen; |
| 252 | + } |
| 253 | + |
| 254 | + // The minimum cost of the last house can be any color |
| 255 | + return min({previousRed, previousBlue, previousGreen}); |
| 256 | + } |
| 257 | +}; |
| 258 | + |
| 259 | +int main() { |
| 260 | + Solution solution; |
| 261 | + |
| 262 | + vector<vector<int>> costs1 = {{17, 2, 17}, {16, 16, 5}, {14, 3, 19}}; |
| 263 | + cout << solution.minCost(costs1) << endl; // Output: 10 |
| 264 | + |
| 265 | + vector<vector<int>> costs2 = {{7, 6, 2}}; |
| 266 | + cout << solution.minCost(costs2) << endl; // Output: 2 |
| 267 | + |
| 268 | + return 0; |
| 269 | +} |
| 270 | +``` |
| 271 | +</TabItem> |
| 272 | +</Tabs> |
| 273 | + |
| 274 | +### Explanation: |
| 275 | + |
| 276 | +<Tabs> |
| 277 | +<TabItem value="javascript" label="JavaScript"> |
| 278 | +- **Initialization**: We start by assigning the costs of painting the first house to `previousRed`, `previousBlue`, and `previousGreen`. |
| 279 | +- **Dynamic Programming**: For each subsequent house, we calculate the cost of painting it each color based on the minimum cost of painting the previous house a different color. |
| 280 | + - `currentRed = costs[i][0] + Math.min(previousBlue, previousGreen)` |
| 281 | + - `currentBlue = costs[i][1] + Math.min(previousRed, previousGreen)` |
| 282 | + - `currentGreen = costs[i][2] + Math.min(previousRed, previousBlue)` |
| 283 | +- **Result**: After processing all the houses, the minimum cost of painting the last house can be either red, blue, or green. The final result is the minimum of these three values. |
| 284 | +</TabItem> |
| 285 | +<TabItem value="typescript" label="TypeScript"> |
| 286 | +- **Initialization**: We start by assigning the costs of painting the first house to `previousRed`, `previousBlue`, and `previousGreen`. |
| 287 | +- **Dynamic Programming**: For each subsequent house, we calculate the cost of painting it each color based on the minimum cost of painting the previous house a different color. |
| 288 | + - `currentRed = costs[i][0] + Math.min(previousBlue, previousGreen)` |
| 289 | + - `currentBlue = costs[i][1] + Math.min(previousRed, previousGreen)` |
| 290 | + - `currentGreen = costs[i][2] + Math.min(previousRed, previousBlue)` |
| 291 | +- **Result**: After processing all the houses, the minimum cost of painting the last house can be either red, blue, or green. The final result is the minimum of these three values. |
| 292 | + |
| 293 | +</TabItem> |
| 294 | +<TabItem value="python" label="Python"> |
| 295 | +1. **Initialization**: |
| 296 | + - Initialize the costs for painting the first house (`previousRed`, `previousBlue`, `previousGreen`) using the first row of the input `costs` matrix. |
| 297 | + |
| 298 | +2. **Dynamic Programming**: |
| 299 | + - Iterate through each house starting from the second one (index 1). For each house, calculate the minimum cost to paint it red, blue, or green based on the costs of painting the previous house a different color. |
| 300 | + - Update the variables to store the minimum costs for the current house to be used in the next iteration. |
| 301 | + |
| 302 | +3. **Result**: |
| 303 | + - After processing all the houses, the result is the minimum cost of painting the last house, which can be either red, blue, or green. |
| 304 | +</TabItem> |
| 305 | +<TabItem value="java" label="Java"> |
| 306 | +1. **Initialization**: |
| 307 | + - Initialize the costs for painting the first house (`previousRed`, `previousBlue`, `previousGreen`) using the first row of the input `costs` matrix. |
| 308 | + |
| 309 | +2. **Dynamic Programming**: |
| 310 | + - Iterate through each house starting from the second one (index 1). For each house, calculate the minimum cost to paint it red, blue, or green based on the costs of painting the previous house a different color. |
| 311 | + - Update the variables to store the minimum costs for the current house to be used in the next iteration. |
| 312 | + |
| 313 | +3. **Result**: |
| 314 | + - After processing all the houses, the result is the minimum cost of painting the last house, which can be either red, blue, or green. |
| 315 | + |
| 316 | +</TabItem> |
| 317 | +<TabItem value="cpp" label="c++"> |
| 318 | +1. **Initialization**: |
| 319 | + - Initialize the costs for painting the first house (`previousRed`, `previousBlue`, `previousGreen`) using the first row of the input `costs` matrix. |
| 320 | + |
| 321 | +2. **Dynamic Programming**: |
| 322 | + - Iterate through each house starting from the second one (index 1). For each house, calculate the minimum cost to paint it red, blue, or green based on the costs of painting the previous house a different color. |
| 323 | + - Update the variables to store the minimum costs for the current house to be used in the next iteration. |
| 324 | + |
| 325 | +3. **Result**: |
| 326 | + - After processing all the houses, the result is the minimum cost of painting the last house, which can be either red, blue, or green. |
| 327 | +</TabItem> |
| 328 | +</Tabs> |
| 329 | + |
| 330 | +### Complexity: |
| 331 | + |
| 332 | +<Tabs> |
| 333 | +<TabItem value="javascript" label="JavaScript" default> |
| 334 | + |
| 335 | +- **Time Complexity**: `O(n)`, where `n` is the number of houses. We only make a single pass through the houses. |
| 336 | +- **Space Complexity**: `O(1)`, as we use a constant amount of additional space regardless of the number of houses. |
| 337 | + |
| 338 | +</TabItem> |
| 339 | +<TabItem value="typescript" label="TypeScript"> |
| 340 | +- **Time Complexity**: `O(n)`, where `n` is the number of houses. We only make a single pass through the houses. |
| 341 | +- **Space Complexity**: `O(1)`, as we use a constant amount of additional space regardless of the number of houses. |
| 342 | +</TabItem> |
| 343 | +<TabItem value="python" label="Python"> |
| 344 | + |
| 345 | +- **Time Complexity**: `O(n)`, where `n` is the number of houses. The solution involves a single pass through the list of houses. |
| 346 | +- **Space Complexity**: `O(1)`, as we use a constant amount of extra space for storing the costs of painting the houses. |
| 347 | + |
| 348 | +</TabItem> |
| 349 | +<TabItem value="java" label="Java"> |
| 350 | + |
| 351 | + |
| 352 | +- **Time Complexity**: `O(n)`, where `n` is the number of houses. The solution involves a single pass through the list of houses. |
| 353 | +- **Space Complexity**: `O(1)`, as we use a constant amount of extra space for storing the costs of painting the houses. |
| 354 | + |
| 355 | + |
| 356 | +</TabItem> |
| 357 | +<TabItem value="cpp" label="c++"> |
| 358 | + |
| 359 | +- **Time Complexity**: `O(n)`, where `n` is the number of houses. The solution involves a single pass through the list of houses. |
| 360 | +- **Space Complexity**: `O(1)`, as we use a constant amount of extra space for storing the costs of painting the houses. |
| 361 | + |
| 362 | +</TabItem> |
| 363 | +</Tabs> |
| 364 | + |
| 365 | +## References |
| 366 | + |
| 367 | +- **LeetCode Problem:** [paint house](https://leetcode.com/problems/paint-house/) |
| 368 | + |
| 369 | +<h2>Author:</h2> |
| 370 | + |
| 371 | +<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}> |
| 372 | +{['sivaprasath2004'].map(username => ( |
| 373 | + <Author key={username} username={username} /> |
| 374 | +))} |
| 375 | +</div> |
0 commit comments