File tree Expand file tree Collapse file tree 2 files changed +52
-1
lines changed Expand file tree Collapse file tree 2 files changed +52
-1
lines changed Original file line number Diff line number Diff line change 32
32
| 37 | [ Sudoku Solver] ( https://leetcode.com/problems/sudoku-solver ) | [ C] ( ./src/37.c ) | Hard |
33
33
| 38 | [ Count and Say] ( https://leetcode.com/problems/count-and-say ) | [ C] ( ./src/38.c ) | Medium |
34
34
| 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 |
35
36
| 50 | [ Pow(x, n)] ( https://leetcode.com/problems/powx-n ) | [ C] ( ./src/50.c ) | Medium |
36
37
| 53 | [ Maximum Subarray] ( https://leetcode.com/problems/maximum-subarray ) | [ C] ( ./src/53.c ) | Medium |
37
38
| 62 | [ Unique Paths] ( https://leetcode.com/problems/unique-paths ) | [ C] ( ./src/62.c ) | Medium |
92
93
| 485 | [ Max Consecutive Ones] ( https://leetcode.com/problems/max-consecutive-ones ) | [ C] ( ./src/485.c ) | Easy |
93
94
| 509 | [ Fibonacci Number] ( https://leetcode.com/problems/fibonacci-number ) | [ C] ( ./src/509.c ) | Easy |
94
95
| 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 |
96
97
| 561 | [ Array Partition] ( https://leetcode.com/problems/array-partition ) | [ C] ( ./src/561.c ) | Easy |
97
98
| 567 | [ Permutation in String] ( https://leetcode.com/problems/permutation-in-string ) | [ C] ( ./src/567.c ) | Medium |
98
99
| 617 | [ Merge Two Binary Trees] ( https://leetcode.com/problems/merge-two-binary-trees ) | [ C] ( ./src/617.c ) | Easy |
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments