Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
61
Task/Pancake-numbers/JavaScript/pancake-numbers.js
Normal file
61
Task/Pancake-numbers/JavaScript/pancake-numbers.js
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
function flipStack(stack, index) {
|
||||
const newStack = [...stack];
|
||||
let start = 0;
|
||||
let end = index - 1;
|
||||
while (start < end) {
|
||||
const temp = newStack[start];
|
||||
newStack[start] = newStack[end];
|
||||
newStack[end] = temp;
|
||||
start++;
|
||||
end--;
|
||||
}
|
||||
return newStack;
|
||||
}
|
||||
function pancake(number) {
|
||||
|
||||
const initialStack = Array.from({ length: number }, (_, i) => i + 1);
|
||||
|
||||
const stackFlips = {};
|
||||
const initialKey = initialStack.join(',');
|
||||
stackFlips[initialKey] = 0;
|
||||
|
||||
const queue = [initialStack];
|
||||
while (queue.length > 0) {
|
||||
const stack = queue.shift();
|
||||
const key = stack.join(',');
|
||||
|
||||
const flips = stackFlips[key] + 1;
|
||||
|
||||
for (let i = 2; i <= number; ++i) {
|
||||
const flipped = flipStack(stack, i);
|
||||
const flippedKey = flipped.join(',');
|
||||
|
||||
if (stackFlips[flippedKey] === undefined) {
|
||||
stackFlips[flippedKey] = flips;
|
||||
queue.push(flipped);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let maxFlips = -1;
|
||||
let worstStackKey = null;
|
||||
for (const key in stackFlips) {
|
||||
if (stackFlips[key] > maxFlips) {
|
||||
maxFlips = stackFlips[key];
|
||||
worstStackKey = key;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
stack: worstStackKey.split(',').map(Number),
|
||||
flips: maxFlips
|
||||
};
|
||||
}
|
||||
|
||||
for (let n = 1; n <= 8; ++n) {
|
||||
const result = pancake(n);
|
||||
|
||||
const flipsStr = String(result.flips).padStart(2, ' ');
|
||||
|
||||
console.log(`pancake(${n}) = ${flipsStr}. Example [${result.stack.join(', ')}]`);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue