Skip to content

Commit 8514d22

Browse files
committed
Add solution #256
1 parent c28a804 commit 8514d22

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@
245245
253|[Meeting Rooms II](./solutions/0253-meeting-rooms-ii.js)|Medium|
246246
254|[Factor Combinations](./solutions/0254-factor-combinations.js)|Medium|
247247
255|[Verify Preorder Sequence in Binary Search Tree](./solutions/0255-verify-preorder-sequence-in-binary-search-tree.js)|Medium|
248+
256|[Paint House](./solutions/0256-paint-house.js)|Medium|
248249
257|[Binary Tree Paths](./solutions/0257-binary-tree-paths.js)|Easy|
249250
258|[Add Digits](./solutions/0258-add-digits.js)|Easy|
250251
260|[Single Number III](./solutions/0260-single-number-iii.js)|Medium|

solutions/0256-paint-house.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 256. Paint House
3+
* https://leetcode.com/problems/paint-house/
4+
* Difficulty: Medium
5+
*
6+
* There is a row of n houses, where each house can be painted one of three colors: red, blue,
7+
* or green. The cost of painting each house with a certain color is different. You have to
8+
* paint all the houses such that no two adjacent houses have the same color.
9+
*
10+
* The cost of painting each house with a certain color is represented by an n x 3 cost matrix
11+
* costs.
12+
* - For example, costs[0][0] is the cost of painting house 0 with the color red; costs[1][2]
13+
* is the cost of painting house 1 with color green, and so on...
14+
*
15+
* Return the minimum cost to paint all houses.
16+
*/
17+
18+
/**
19+
* @param {number[][]} costs
20+
* @return {number}
21+
*/
22+
var minCost = function(costs) {
23+
let prev = [...costs[0]];
24+
25+
for (let i = 1; i < costs.length; i++) {
26+
prev = [
27+
costs[i][0] + Math.min(prev[1], prev[2]),
28+
costs[i][1] + Math.min(prev[0], prev[2]),
29+
costs[i][2] + Math.min(prev[0], prev[1])
30+
];
31+
}
32+
33+
return Math.min(...prev);
34+
};

0 commit comments

Comments
 (0)