Skip to content

Commit d8dcb4d

Browse files
committed
Add solution #1473
1 parent 42f35ff commit d8dcb4d

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,318 LeetCode solutions in JavaScript
1+
# 1,319 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1126,6 +1126,7 @@
11261126
1470|[Shuffle the Array](./solutions/1470-shuffle-the-array.js)|Easy|
11271127
1471|[The k Strongest Values in an Array](./solutions/1471-the-k-strongest-values-in-an-array.js)|Medium|
11281128
1472|[Design Browser History](./solutions/1472-design-browser-history.js)|Medium|
1129+
1473|[Paint House III](./solutions/1473-paint-house-iii.js)|Hard|
11291130
1475|[Final Prices With a Special Discount in a Shop](./solutions/1475-final-prices-with-a-special-discount-in-a-shop.js)|Easy|
11301131
1480|[Running Sum of 1d Array](./solutions/1480-running-sum-of-1d-array.js)|Easy|
11311132
1481|[Least Number of Unique Integers after K Removals](./solutions/1481-least-number-of-unique-integers-after-k-removals.js)|Medium|

solutions/1473-paint-house-iii.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* 1473. Paint House III
3+
* https://leetcode.com/problems/paint-house-iii/
4+
* Difficulty: Hard
5+
*
6+
* There is a row of m houses in a small city, each house must be painted with one of the n
7+
* colors (labeled from 1 to n), some houses that have been painted last summer should not
8+
* be painted again.
9+
*
10+
* A neighborhood is a maximal group of continuous houses that are painted with the same color.
11+
*
12+
* For example: houses = [1,2,2,3,3,2,1,1] contains 5 neighborhoods [{1}, {2,2}, {3,3}, {2}, {1,1}].
13+
*
14+
* Given an array houses, an m x n matrix cost and an integer target where:
15+
* - houses[i]: is the color of the house i, and 0 if the house is not painted yet.
16+
* - cost[i][j]: is the cost of paint the house i with the color j + 1.
17+
*
18+
* Return the minimum cost of painting all the remaining houses in such a way that there are exactly
19+
* target neighborhoods. If it is not possible, return -1.
20+
*/
21+
22+
/**
23+
* @param {number[]} houses
24+
* @param {number[][]} cost
25+
* @param {number} m
26+
* @param {number} n
27+
* @param {number} target
28+
* @return {number}
29+
*/
30+
var minCost = function(houses, cost, m, n, target) {
31+
const cache = new Map();
32+
const result = findMinCost(0, 0, 0);
33+
return result === Infinity ? -1 : result;
34+
35+
function findMinCost(index, prevColor, neighborhoods) {
36+
if (index === m) return neighborhoods === target ? 0 : Infinity;
37+
if (neighborhoods > target) return Infinity;
38+
39+
const key = `${index}:${prevColor}:${neighborhoods}`;
40+
if (cache.has(key)) return cache.get(key);
41+
42+
let minCost = Infinity;
43+
const currentColor = houses[index];
44+
45+
if (currentColor !== 0) {
46+
const newNeighborhoods = prevColor === currentColor ? neighborhoods : neighborhoods + 1;
47+
minCost = findMinCost(index + 1, currentColor, newNeighborhoods);
48+
} else {
49+
for (let color = 1; color <= n; color++) {
50+
const newNeighborhoods = prevColor === color ? neighborhoods : neighborhoods + 1;
51+
const currentCost = cost[index][color - 1]
52+
+ findMinCost(index + 1, color, newNeighborhoods);
53+
minCost = Math.min(minCost, currentCost);
54+
}
55+
}
56+
57+
cache.set(key, minCost);
58+
return minCost;
59+
}
60+
};

0 commit comments

Comments
 (0)