mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
3.7 KiB
3.7 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 69f35a5bb823ed620fcb7cb8 | Challenge 279: Longest Domino Chain | 28 | challenge-279 |
--description--
Given a 2D array representing a set of dominoes, return the longest valid chain.
- Each domino is a pair of numbers from 0–6, e.g.
[3, 2]. - A chain is valid when the second number of each domino matches the first number of the next.
- The first number of the first domino and the second number of the last one don't need to match anything.
- Any domino can be flipped, so
[3, 2]can be played as[2, 3]. - There is always exactly one longest valid chain.
For example, given [[1, 2], [4, 5], [2, 3]], return [[1, 2], [2, 3]].
--hints--
getLongestChain([[1, 2], [4, 5], [2, 3]]) should return [[1, 2], [2, 3]].
const chain = JSON.stringify(getLongestChain([[1, 2], [4, 5], [2, 3]]));
const result1 = JSON.stringify([[1, 2], [2, 3]]);
const result2 = JSON.stringify([[3, 2], [2, 1]]);
assert.isTrue(chain === result1 || chain === result2);
getLongestChain([[2, 1], [4, 3], [5, 3]]) should return [[4, 3], [3, 5]].
const chain = JSON.stringify(getLongestChain([[2, 1], [4, 3], [5, 3]]));
const result1 = JSON.stringify([[4, 3], [3, 5]]);
const result2 = JSON.stringify([[5, 3], [3, 4]]);
assert.isTrue(chain === result1 || chain === result2);
getLongestChain([[1, 2], [3, 4], [2, 3], [4, 0]]) should return [[1, 2], [2, 3], [3, 4], [4, 0]].
const chain = JSON.stringify(getLongestChain([[1, 2], [3, 4], [2, 3], [4, 0]]));
const result1 = JSON.stringify([[1, 2], [2, 3], [3, 4], [4, 0]]);
const result2 = JSON.stringify([[0, 4], [4, 3], [3, 2], [2, 1]]);
assert.isTrue(chain === result1 || chain === result2);
getLongestChain([[6, 6], [6, 1], [1, 1], [0, 3], [2, 3], [4, 1], [5, 6]]) should return [[4, 1], [1, 1], [1, 6], [6, 6], [6, 5]].
const chain = JSON.stringify(getLongestChain([[6, 6], [6, 1], [1, 1], [0, 3], [2, 3], [4, 1], [5, 6]]));
const result1 = JSON.stringify([[4, 1], [1, 1], [1, 6], [6, 6], [6, 5]]);
const result2 = JSON.stringify([[5, 6], [6, 6], [6, 1], [1, 1], [1, 4]]);
assert.isTrue(chain === result1 || chain === result2);
getLongestChain([[0, 4], [3, 3], [0, 3], [5, 6], [4, 5], [4, 2], [5, 5], [1, 2], [4, 4]]) should return [[3, 3], [3, 0], [0, 4], [4, 4], [4, 5], [5, 5], [5, 6]].
const chain = JSON.stringify(getLongestChain([[0, 4], [3, 3], [0, 3], [5, 6], [4, 5], [4, 2], [5, 5], [1, 2], [4, 4]]));
const result1 = JSON.stringify([[3, 3], [3, 0], [0, 4], [4, 4], [4, 5], [5, 5], [5, 6]]);
const result2 = JSON.stringify([[6, 5], [5, 5], [5, 4], [4, 4], [4, 0], [0, 3], [3, 3]]);
assert.isTrue(chain === result1 || chain === result2);
--seed--
--seed-contents--
function getLongestChain(dominoes) {
return dominoes;
}
--solutions--
function getLongestChain(dominoes) {
function search(chain, remaining) {
let best = chain;
const last = chain[chain.length - 1][1];
for (let i = 0; i < remaining.length; i++) {
const [a, b] = remaining[i];
const rest = remaining.filter((_, j) => j !== i);
if (a === last) {
const result = search([...chain, [a, b]], rest);
if (result.length > best.length) best = result;
}
if (b === last && a !== b) {
const result = search([...chain, [b, a]], rest);
if (result.length > best.length) best = result;
}
}
return best;
}
let best = [];
for (let i = 0; i < dominoes.length; i++) {
const [a, b] = dominoes[i];
const rest = dominoes.filter((_, j) => j !== i);
const r1 = search([[a, b]], rest);
if (r1.length > best.length) best = r1;
if (a !== b) {
const r2 = search([[b, a]], rest);
if (r2.length > best.length) best = r2;
}
}
return best;
}