Files
wehub-resource-sync 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
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

92 lines
2.3 KiB
Markdown

---
id: 6a0dcd03ee4e68698080ef68
title: "Challenge 304: Itinerary Arrangements"
challengeType: 28
dashedName: 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`.
```js
assert.equal(getItineraryCount(["library", "park"]), 2);
```
`getItineraryCount(["library", "park", "arcade"])` should return `18`.
```js
assert.equal(getItineraryCount(["library", "park", "arcade"]), 18);
```
`getItineraryCount(["library", "park", "arcade", "store"])` should return `120`.
```js
assert.equal(getItineraryCount(["library", "park", "arcade", "store"]), 120);
```
`getItineraryCount(["library", "park", "arcade", "store", "cafe"])` should return `840`.
```js
assert.equal(getItineraryCount(["library", "park", "arcade", "store", "cafe"]), 840);
```
`getItineraryCount(["library", "park", "arcade", "store", "cafe", "market", "museum"])` should return `55440`.
```js
assert.equal(getItineraryCount(["library", "park", "arcade", "store", "cafe", "market", "museum"]), 55440);
```
# --seed--
## --seed-contents--
```js
function getItineraryCount(stops) {
return stops;
}
```
# --solutions--
```js
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;
}
```