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 <halfpacho@gmail.com>

* Update leetcode/src/45.c

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update leetcode/src/45.c

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update leetcode/src/45.c

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update leetcode/src/45.c

Co-authored-by: David Leal <halfpacho@gmail.com>

* updating DIRECTORY.md

---------

Co-authored-by: github-actions[bot] <github-actions@users.noreply.github.com>
Co-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
Alexander Pantyukhin 2023-02-28 10:55:09 +04:00 committed by GitHub
parent 59dc816c9d
commit 0bc8f7a576
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 1 deletions

View File

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

50
leetcode/src/45.c Normal file
View File

@ -0,0 +1,50 @@
// Breadth-first search, imitation.
// Runtime: O(n)
// Space: O(n)
int jump(int* nums, int numsSize) {
if (numsSize == 1) {
return 0;
}
int step = 1;
int* visitedCells = calloc(numsSize, sizeof(int));
int* queue = malloc(numsSize * sizeof(int));
queue[0] = 0;
int queueLength = 1;
while (queueLength > 0){
int* nextQueue = malloc(numsSize * sizeof(int));
int nextQueueLength = 0;
for (int i = 0; i < queueLength; i++) {
int cell = queue[i];
int jump = nums[cell];
if (cell + jump >= numsSize - 1) {
free(visitedCells);
free(queue);
free(nextQueue);
return step;
}
// populate next queue wave for searching
for (int nextCell = cell; nextCell <= cell + jump; nextCell++) {
if (visitedCells[nextCell] == 0){
nextQueue[nextQueueLength++] = nextCell;
visitedCells[nextCell] = 1;
}
}
}
step++;
free(queue);
queue = nextQueue;
queueLength = nextQueueLength;
}
free(visitedCells);
free(queue);
return -1;
}