Solución
solution.tsTypeScript
function minPaintCost(costs: number[][]): number {
if (costs.length === 0) return 0;
if (costs[0].length === 0) return 0;
let previousMin = 0;
let previousSecondMin = 0;
let previousMinColorIndex = -1;
for (let house of costs) {
let currentMin = Infinity;
let currentSecondMin = Infinity;
for (const [paintCostIndex, paintCost] of house.entries()) {
const bestPrevCost = paintCostIndex === previousMinColorIndex ? previousSecondMin : previousMin;
const totalCost = paintCost + bestPrevCost;
if (totalCost < currentMin) {
currentSecondMin = currentMin;
currentMin = totalCost;
previousMinColorIndex = paintCostIndex;
}
else if (totalCost < currentSecondMin) {
currentSecondMin = totalCost;
}
}
previousMin = currentMin;
previousSecondMin = currentSecondMin;
}
return previousMin;
}
export { minPaintCost };0respuestas