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
commit 92f04903f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 0 deletions

@ -121,6 +121,16 @@ The list contains the name of (i.e. the value returned by
| | | Assuming you are initially positioned at the start of the array, determine |
| | | whether you are able to reach the last index of the array. |
+------------------------------------+------------------------------------------------------------------------------------------+
| Array Jumping Game II | | You are given an array of integers where each element represents the |
| | | maximum possible jump distance from that position. For example, if you |
| | | are at position i and your maximum jump length is n, then you can jump |
| | | to any position from i to i+n. |
| | | |
| | | Assuming you are initially positioned at the start of the array, determine |
| | | the minimum number of jumps to reach the end of the array. |
| | | |
| | | If it's impossible to reach the end, then the answer should be 0. |
+------------------------------------+------------------------------------------------------------------------------------------+
| Merge Overlapping Intervals | | Given an array of intervals, merge all overlapping intervals. An interval |
| | | is an array with two numbers, where the first number is always less than |
| | | the second (e.g. [1, 5]). |

@ -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 [