Published
Edited
Jan 3, 2022
1 star
Insert cell
# Trash Car
Insert cell
solution([3, 2, 4], ["MPM", "", "G"])
Insert cell
solution([2, 1, 1, 1, 2], ['', 'PP', 'PP', 'GM', ''])
Insert cell
solution([2,5], ['PGP', 'M'])
Insert cell
function solution(distance, trashes) {
const trashMap = new Map();
for (const trash of trashes) {
for (const el of trash) trashMap.set(el, trashMap.get(el) + 1 || 1);
}

const houses = [];
for (let i = 0; i < distance.length; i++) {
const trashMap = new Map();
for (const trash of trashes[i]) {
trashMap.set(trash, trashMap.get(trash) + 1 || 1);
}
houses.push({ distance: distance[i], trashes: trashMap });
}

const collectionTimes = [
collect(houses, "P", trashMap),
collect(houses, "G", trashMap),
collect(houses, "M", trashMap)
];

return Math.max(...collectionTimes);
}
Insert cell
function collect(houses, type, trashMap) {
let time = 0;
let pos = -1;
let distance = 0;

while (trashMap.has(type)) {
pos++;
const house = houses[pos];
distance += house.distance;
time += house.distance;

if (house.trashes.has(type)) {
const houseTrash = house.trashes.get(type);
time += houseTrash;
house.trashes.delete(type);

const trashCount = trashMap.get(type) - houseTrash;
if (trashCount < 1) {
trashMap.delete(type);
} else {
trashMap.set(type, trashCount);
}
}
}

return (time += distance);
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more