Published
Edited
Jan 12, 2022
Insert cell
# Gym clothes
Insert cell
function solution(n, lost, reserve) {
const losts = lost.sort((a, b) => a - b);
const reserves = reserve.sort((a, b) => a - b);

let reserveCount = 0;
const set = new Set(reserves);
for (const [idx, val] of Object.entries(losts)) {
if (set.has(val)) {
set.delete(val);
losts[idx] = null;
reserveCount++;
}
}

for (const l of losts) {
switch (true) {
case set.has(l):
reserveCount++;
set.delete(l);
break;
case set.has(l - 1):
reserveCount++;
set.delete(l - 1);
break;
case set.has(l + 1):
reserveCount++;
set.delete(l + 1);
break;
}
}

return n - (lost.length - reserveCount);
}
Insert cell
solution(5, [2, 4], [1, 3, 5])
Insert cell
solution(5, [2, 4], [3])
Insert cell
solution(3, [3], [1])
Insert cell
solution(5, [2, 3], [3, 4])
Insert cell
solution(3, [1, 2], [2, 3])
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