最小跳跃次数
难度:
标签:
题目描述
English description is not available for the problem. Please switch to Chinese.
代码结果
运行时间: 244 ms, 内存: 79.4 MB
/*
* 思路:
* 1. 通过使用队列来实现BFS来找到最小按动次数。
* 2. 由于Java Stream不适合在此场景下实现BFS,因此保持逻辑不变。
* 3. 仍然是计算向右和向左弹射的所有可能性。
*/
import java.util.stream.IntStream;
import java.util.LinkedList;
import java.util.Queue;
public class SpringGameStream {
public int minJumps(int[] jump) {
int n = jump.length;
boolean[] visited = new boolean[n];
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{0, 0});
visited[0] = true;
int maxReach = 0;
while (!queue.isEmpty()) {
int[] current = queue.poll();
int index = current[0];
int step = current[1];
int nextIndex = index + jump[index];
if (nextIndex >= n) {
return step + 1;
}
if (!visited[nextIndex]) {
visited[nextIndex] = true;
queue.offer(new int[]{nextIndex, step + 1});
}
final int finalMaxReach = maxReach;
IntStream.range(finalMaxReach, index).filter(i -> !visited[i]).forEach(i -> {
visited[i] = true;
queue.offer(new int[]{i, step + 1});
});
maxReach = Math.max(maxReach, index);
}
return -1;
}
}
解释
方法:
The solution uses a breadth-first search (BFS) strategy, where each position on the springboard array 'jump' is treated as a node in a graph, and the possible jumps from each node define the edges. Starting from the first spring (index 0), the algorithm explores all possible jumps (both forward to 'i + jump[i]' and backward to any index less than 'i'). A queue is used to keep track of the next indices to visit. The 'visited_status' set ensures each index is only added once to the queue to prevent infinite loops. The BFS approach guarantees that the first time the algorithm reaches beyond the last index (N), the minimum number of moves has been found.
时间复杂度:
O(n)
空间复杂度:
O(n)
代码细节讲解
🦆
在解决此问题时,为什么选择使用宽度优先搜索(BFS)而不是深度优先搜索(DFS)或其他方法?
▷🦆
代码中提到的`visited_status`集合是如何帮助防止无限循环的?具体是通过什么机制实现的?
▷🦆
为什么需要一个单独的变量`left_max_vis_idx`来跟踪访问过的最左边的索引?这与普通的BFS有什么不同?
▷