dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
2.3 KiB
2.3 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 6a0dcd03ee4e68698080ef68 | Challenge 304: Itinerary Arrangements | 28 | challenge-304 |
--description--
Given an array of at least two optional stops for a day trip, return the number of valid itinerary arrangements.
The itinerary always includes "breakfast", "lunch", and "dinner", these will not be passed in as arguments. The optional stops can be placed anywhere in the itinerary, subject to the following rules:
"breakfast"is always first, with at least one stop before"lunch"."lunch"must appear before"dinner", with at least one stop in between.- At most, one optional stop may appear after
"dinner".
Return the number of valid arrangements.
--hints--
getItineraryCount(["library", "park"]) should return 2.
assert.equal(getItineraryCount(["library", "park"]), 2);
getItineraryCount(["library", "park", "arcade"]) should return 18.
assert.equal(getItineraryCount(["library", "park", "arcade"]), 18);
getItineraryCount(["library", "park", "arcade", "store"]) should return 120.
assert.equal(getItineraryCount(["library", "park", "arcade", "store"]), 120);
getItineraryCount(["library", "park", "arcade", "store", "cafe"]) should return 840.
assert.equal(getItineraryCount(["library", "park", "arcade", "store", "cafe"]), 840);
getItineraryCount(["library", "park", "arcade", "store", "cafe", "market", "museum"]) should return 55440.
assert.equal(getItineraryCount(["library", "park", "arcade", "store", "cafe", "market", "museum"]), 55440);
--seed--
--seed-contents--
function getItineraryCount(stops) {
return stops;
}
--solutions--
function getItineraryCount(stops) {
const items = ["lunch", "dinner", ...stops];
let count = 0;
function permute(arr) {
if (arr.length === 0) return [[]];
return arr.flatMap((item, i) =>
permute([...arr.slice(0, i), ...arr.slice(i + 1)]).map(p => [item, ...p])
);
}
for (const perm of permute(items)) {
const lunchIdx = perm.indexOf("lunch");
const dinnerIdx = perm.indexOf("dinner");
if (lunchIdx >= dinnerIdx) continue;
if (lunchIdx < 1) continue;
if (dinnerIdx - lunchIdx < 2) continue;
if (perm.length - dinnerIdx - 1 > 1) continue;
count++;
}
return count;
}