Merge pull request #3385 from anatoly-kussul/cct-array-jumping-game-2

New coding contract type: Array Jumping Game II
This commit is contained in:
hydroflame
2022-04-11 13:21:36 -04:00
committed by GitHub
2 changed files with 66 additions and 0 deletions
+56
View File
@@ -357,6 +357,62 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
return (ans === "1" && solution) || (ans === "0" && !solution);
},
},
{
desc: (arr: number[]): string => {
return [
"You are given the following array of integers:\n\n",
`${arr}\n\n`,
"Each element in the array represents your MAXIMUM jump length",
"at that position. This means that if you are at position i and your",
"maximum jump length is n, you can jump to any position from",
"i to i+n.",
"\n\nAssuming you are initially positioned",
"at the start of the array, determine the minimum number of",
"jumps to reach the end of the array.\n\n",
"If it's impossible to reach the end, then the answer should be 0.",
].join(" ");
},
difficulty: 3,
gen: (): number[] => {
const len: number = getRandomInt(3, 25);
const arr: number[] = [];
arr.length = len;
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < 10; j++) {
if (Math.random() <= j / 10 + 0.1) {
arr[i] = j;
break;
}
}
}
return arr;
},
name: "Array Jumping Game II",
numTries: 3,
solver: (data: number[], ans: string): boolean => {
const n: number = data.length;
let reach = 0;
let jumps = 0;
let lastJump = -1;
while (reach < n - 1) {
let jumpedFrom = -1;
for (let i = reach; i > lastJump; i--) {
if (i + data[i] > reach) {
reach = i + data[i];
jumpedFrom = i;
}
}
if (jumpedFrom === -1) {
jumps = 0;
break;
}
lastJump = jumpedFrom;
jumps++;
}
return jumps === parseInt(ans, 10);
},
},
{
desc: (arr: number[][]): string => {
return [