diff --git a/doc/source/basicgameplay/codingcontracts.rst b/doc/source/basicgameplay/codingcontracts.rst index 3edb89453..4b26f6828 100644 --- a/doc/source/basicgameplay/codingcontracts.rst +++ b/doc/source/basicgameplay/codingcontracts.rst @@ -115,6 +115,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]). | diff --git a/src/data/codingcontracttypes.ts b/src/data/codingcontracttypes.ts index bbdc17ba3..f1553f775 100644 --- a/src/data/codingcontracttypes.ts +++ b/src/data/codingcontracttypes.ts @@ -312,6 +312,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 [