Skip to content

Commit 0bc8f7a

Browse files
alexpantyukhingithub-actions[bot]Panquesito7
authored
feat: add LeetCode jump game II (#1213)
* add leetcode Jump Game II * updating DIRECTORY.md * Update 45.c free correct resources * Update leetcode/src/45.c Co-authored-by: David Leal <[email protected]> * Update leetcode/src/45.c Co-authored-by: David Leal <[email protected]> * Update leetcode/src/45.c Co-authored-by: David Leal <[email protected]> * Update leetcode/src/45.c Co-authored-by: David Leal <[email protected]> * Update leetcode/src/45.c Co-authored-by: David Leal <[email protected]> * updating DIRECTORY.md --------- Co-authored-by: github-actions[bot] <[email protected]> Co-authored-by: David Leal <[email protected]>
1 parent 59dc816 commit 0bc8f7a

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

leetcode/DIRECTORY.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
| 37 | [Sudoku Solver](https://leetcode.com/problems/sudoku-solver) | [C](./src/37.c) | Hard |
3333
| 38 | [Count and Say](https://leetcode.com/problems/count-and-say) | [C](./src/38.c) | Medium |
3434
| 42 | [Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water) | [C](./src/42.c) | Hard |
35+
| 45 | [Jump Game II](https://leetcode.com/problems/jump-game-ii) | [C](./src/45.c) | Medium |
3536
| 50 | [Pow(x, n)](https://leetcode.com/problems/powx-n) | [C](./src/50.c) | Medium |
3637
| 53 | [Maximum Subarray](https://leetcode.com/problems/maximum-subarray) | [C](./src/53.c) | Medium |
3738
| 62 | [Unique Paths](https://leetcode.com/problems/unique-paths) | [C](./src/62.c) | Medium |
@@ -92,7 +93,7 @@
9293
| 485 | [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones) | [C](./src/485.c) | Easy |
9394
| 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number) | [C](./src/509.c) | Easy |
9495
| 520 | [Detect Capital](https://leetcode.com/problems/detect-capital) | [C](./src/520.c) | Easy |
95-
| 540 | [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) | [C](./src/540.c) | Medium |
96+
| 540 | [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array) | [C](./src/540.c) | Medium |
9697
| 561 | [Array Partition](https://leetcode.com/problems/array-partition) | [C](./src/561.c) | Easy |
9798
| 567 | [Permutation in String](https://leetcode.com/problems/permutation-in-string) | [C](./src/567.c) | Medium |
9899
| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees) | [C](./src/617.c) | Easy |

leetcode/src/45.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Breadth-first search, imitation.
2+
// Runtime: O(n)
3+
// Space: O(n)
4+
int jump(int* nums, int numsSize) {
5+
if (numsSize == 1) {
6+
return 0;
7+
}
8+
9+
int step = 1;
10+
int* visitedCells = calloc(numsSize, sizeof(int));
11+
12+
int* queue = malloc(numsSize * sizeof(int));
13+
queue[0] = 0;
14+
int queueLength = 1;
15+
16+
while (queueLength > 0){
17+
int* nextQueue = malloc(numsSize * sizeof(int));
18+
int nextQueueLength = 0;
19+
20+
for (int i = 0; i < queueLength; i++) {
21+
int cell = queue[i];
22+
int jump = nums[cell];
23+
24+
if (cell + jump >= numsSize - 1) {
25+
free(visitedCells);
26+
free(queue);
27+
free(nextQueue);
28+
return step;
29+
}
30+
31+
// populate next queue wave for searching
32+
for (int nextCell = cell; nextCell <= cell + jump; nextCell++) {
33+
if (visitedCells[nextCell] == 0){
34+
nextQueue[nextQueueLength++] = nextCell;
35+
visitedCells[nextCell] = 1;
36+
}
37+
}
38+
}
39+
40+
step++;
41+
free(queue);
42+
43+
queue = nextQueue;
44+
queueLength = nextQueueLength;
45+
}
46+
47+
free(visitedCells);
48+
free(queue);
49+
return -1;
50+
}

0 commit comments

Comments
 (0)