Skip to content

Commit 44eab0b

Browse files
committed
problem: 0045. Jump Game II
1 parent 9747749 commit 44eab0b

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

src/0045/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# 0045. [Jump Game II]()
2+
3+
You are given a 0-indexed array of integers `nums` of length `n`. You are initially positioned at `nums[0]`.
4+
5+
Each element `nums[i]` represents the maximum length of a forward jump from index `i`. In other words, if you are at `nums[i]`, you can jump to any `nums[i + j]` where:
6+
7+
- `0 <= j <= nums[i]` and
8+
- `i + j < n`
9+
10+
Return the minimum number of jumps to reach `nums[n - 1]`. The test cases are generated such that you can reach `nums[n - 1]`.
11+
12+
### **Example 1:**
13+
14+
<pre><code>
15+
<strong>Input:</strong> nums = [2,3,1,1,4]
16+
<strong>Output:</strong> 2
17+
<strong>Explanation:</strong> The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.
18+
</code></pre>
19+
20+
### **Example 2:**
21+
22+
<pre><code>
23+
<strong>Input:</strong> nums = [2,3,0,1,4]
24+
<strong>Output:</strong> 2
25+
</code></pre>
26+
27+
### **Constraints:**
28+
29+
- <code>1 <= nums.length <= 10<sup>4</sup></code>
30+
- <code>0 <= nums[i] <= 1000</code>
31+
- It's guaranteed that you can reach `nums[n - 1]`.

src/0045/index.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { jump } from '.';
2+
3+
describe('0045. Jump Game II', () => {
4+
interface TestCase {
5+
input: { nums: number[] };
6+
output: number;
7+
}
8+
9+
const testCases: TestCase[] = [
10+
{
11+
input: {
12+
nums: [2, 3, 1, 1, 4],
13+
},
14+
output: 2,
15+
},
16+
{
17+
input: {
18+
nums: [2, 3, 0, 1, 4],
19+
},
20+
output: 2,
21+
},
22+
];
23+
24+
test.each(testCases)('Case %#', ({ input: { nums }, output }) => {
25+
const relsult = jump(nums);
26+
expect(result).toStrictEqual(output);
27+
});
28+
});

src/0045/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function jump(nums: number[]): number {}
2+
3+
export { jump };

0 commit comments

Comments
 (0)