forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaint House III.js
33 lines (27 loc) · 1.26 KB
/
Paint House III.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
var minCost = function(houses, cost, m, n, target) {
let map = new Map();
function dfs(idx = 0, prevColor = -1, neighborhoods = 0) {
if (idx === m) return neighborhoods === target ? 0 : Infinity;
let key = `${idx}-${prevColor}-${neighborhoods}`;
if (map.has(key)) return map.get(key);
let color = houses[idx];
// If the current house is already painted
if (color > 0) {
return color !== prevColor ? dfs(idx + 1, color, neighborhoods + 1) : dfs(idx + 1, color, neighborhoods);
}
let minCost = Infinity;
for (let i = 1; i <= n; i++) {
let potentialCost;
// If color i is !== prevColor, increment the neighborhood count
if (i !== prevColor) potentialCost = dfs(idx + 1, i, neighborhoods + 1);
// Otherwise, the neighborhood simply expanded so the neighborhood count stays the same
else potentialCost = dfs(idx + 1, i, neighborhoods);
if (potentialCost === Infinity) continue;
minCost = Math.min(minCost, cost[idx][i - 1] + potentialCost);
}
map.set(key, minCost);
return minCost;
}
let minCost = dfs();
return minCost === Infinity ? -1 : minCost;
};