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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more