Skip to content

Commit 0024d62

Browse files
committed
Added brute force to jump game 2
1 parent 1737b21 commit 0024d62

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed

Jump Game II - Leetcode 45/Jump Game II - Leetcode 45.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,36 @@
1+
// Brute Force
2+
#include <vector>
3+
#include <algorithm>
4+
#include <climits>
5+
6+
class Solution {
7+
public:
8+
int jump(std::vector<int>& nums) {
9+
int n = nums.size();
10+
int smallest = INT_MAX;
11+
12+
backtrack(0, 0, nums, n, smallest);
13+
return smallest;
14+
}
15+
16+
private:
17+
void backtrack(int i, int jumps, std::vector<int>& nums, int n, int& smallest) {
18+
if (i == n - 1) {
19+
smallest = std::min(smallest, jumps);
20+
return;
21+
}
22+
23+
int maxJump = nums[i];
24+
int maxReachableIndex = std::min(i + maxJump, n - 1);
25+
26+
for (int newIndex = maxReachableIndex; newIndex > i; newIndex--) {
27+
backtrack(newIndex, jumps + 1, nums, n, smallest);
28+
}
29+
}
30+
};
31+
32+
33+
// Optimal
134
#include <vector>
235
#include <algorithm>
336

Jump Game II - Leetcode 45/Jump Game II - Leetcode 45.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1+
// Brute Force
2+
public class Solution {
3+
public int jump(int[] nums) {
4+
int n = nums.length;
5+
int[] smallest = {Integer.MAX_VALUE};
6+
7+
backtrack(0, 0, nums, n, smallest);
8+
return smallest[0];
9+
}
10+
11+
private void backtrack(int i, int jumps, int[] nums, int n, int[] smallest) {
12+
if (i == n - 1) {
13+
smallest[0] = Math.min(smallest[0], jumps);
14+
return;
15+
}
16+
17+
int maxJump = nums[i];
18+
int maxReachableIndex = Math.min(i + maxJump, n - 1);
19+
20+
for (int newIndex = maxReachableIndex; newIndex > i; newIndex--) {
21+
backtrack(newIndex, jumps + 1, nums, n, smallest);
22+
}
23+
}
24+
}
25+
26+
27+
// Optimal
128
class Solution {
229
public int jump(int[] nums) {
330
int smallest = 0;

Jump Game II - Leetcode 45/Jump Game II - Leetcode 45.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1+
// Brute Force
2+
var jump = function(nums) {
3+
var n = nums.length;
4+
var smallest = [Infinity];
5+
6+
function backtrack(i, jumps) {
7+
if (i === n - 1) {
8+
smallest[0] = Math.min(smallest[0], jumps);
9+
return;
10+
}
11+
12+
var maxJump = nums[i];
13+
var maxReachableIndex = Math.min(i + maxJump, n - 1);
14+
15+
for (var newIndex = maxReachableIndex; newIndex > i; newIndex--) {
16+
backtrack(newIndex, jumps + 1);
17+
}
18+
}
19+
20+
backtrack(0, 0);
21+
return smallest[0];
22+
};
23+
24+
25+
// Optimal
126
var jump = function(nums) {
227
var smallest = 0;
328
var n = nums.length;

Jump Game II - Leetcode 45/Jump Game II - Leetcode 45.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
# Brute Force
2+
class Solution:
3+
def jump(self, nums: List[int]) -> int:
4+
n = len(nums)
5+
smallest = [float('inf')]
6+
7+
def backtrack(i=0, jumps=0):
8+
if i == n-1:
9+
smallest[0] = min(smallest[0], jumps)
10+
return
11+
12+
max_jump = nums[i]
13+
max_reachable_index = min(i+max_jump, n-1)
14+
15+
for new_index in range(max_reachable_index, i, -1):
16+
backtrack(new_index, jumps+1)
17+
18+
backtrack()
19+
return smallest[0]
20+
21+
22+
# Optimal
123
class Solution:
224
def jump(self, nums: List[int]) -> int:
325
smallest = 0

0 commit comments

Comments
 (0)