From 0bc8f7a5766e509d191ae8a2ac616fdfa214d49e Mon Sep 17 00:00:00 2001 From: Alexander Pantyukhin Date: Tue, 28 Feb 2023 10:55:09 +0400 Subject: [PATCH] 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 * Update leetcode/src/45.c Co-authored-by: David Leal * Update leetcode/src/45.c Co-authored-by: David Leal * Update leetcode/src/45.c Co-authored-by: David Leal * Update leetcode/src/45.c Co-authored-by: David Leal * updating DIRECTORY.md --------- Co-authored-by: github-actions[bot] Co-authored-by: David Leal --- leetcode/DIRECTORY.md | 3 ++- leetcode/src/45.c | 50 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 leetcode/src/45.c diff --git a/leetcode/DIRECTORY.md b/leetcode/DIRECTORY.md index c08cde11..61afbda4 100644 --- a/leetcode/DIRECTORY.md +++ b/leetcode/DIRECTORY.md @@ -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 | diff --git a/leetcode/src/45.c b/leetcode/src/45.c new file mode 100644 index 00000000..4deaab45 --- /dev/null +++ b/leetcode/src/45.c @@ -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; +}