到家的最少跳跃次数

231 阅读2分钟

1654. 到家的最少跳跃次数 - 力扣(LeetCode)

有一只跳蚤的家在数轴上的位置  x  处。请你帮助它从位置  0  出发,到达它的家。

跳蚤跳跃的规则如下:

  • 它可以 往前 跳恰好 a  个位置(即往右跳)。
  • 它可以 往后  跳恰好 b  个位置(即往左跳)。
  • 它不能 连续 往后跳 2 次。
  • 它不能跳到任何  forbidden  数组中的位置。

跳蚤可以往前跳 超过  它的家的位置,但是它 不能跳到负整数  的位置。

给你一个整数数组  forbidden ,其中  forbidden[i]  是跳蚤不能跳到的位置,同时给你整数  a, b  和  x ,请你返回跳蚤到家的最少跳跃次数。如果没有恰好到达 x  的可行方案,请你返回 -1

示例 1:

输入: forbidden = [14,4,18,1,15], a = 3, b = 15, x = 9
输出: 3
解释: 往前跳 3 次(0 -> 3 -> 6 -> 9),跳蚤就到家了。

示例 2:

输入: forbidden = [8,3,16,6,12,20], a = 15, b = 13, x = 11
输出: -1

示例 3:

输入: forbidden = [1,6,2,14,5,17,4], a = 16, b = 9, x = 7
输出: 2
解释: 往前跳一次(0 -> 16),然后往回跳一次(16 -> 7),跳蚤就到家了。

提示:

  • 1 <= forbidden.length <= 1000
  • 1 <= a, b, forbidden[i] <= 2000
  • 0 <= x <= 2000
  • forbidden  中所有位置互不相同。
  • 位置  x  不在 forbidden  中。

思路

我们可以使用广度优先遍历进行求解。求解本题,我们先要确定遍历的上限和下限(如果跳蚤可以回家,最少跳跃次数可以达到的的最远点在哪儿)。可以分三种情况:

  • a == b,最远点就是 x
  • a > b,最远点是 x + b,此时在回跳一次即可回家。
  • a < b,最远点是 max(max(forbidden) + a + b, x)

假设最远点为 upper,第 i 次跳跃的位置为 p

  • 如果 p == x,跳跃完成,直接返回。
  • 如果 p < 0 || p > upper || forbidden.includes(p),跳跃不合法
  • 如果当前跳跃是往前跳,下次跳只能往后跳,位置为 p + a
  • 如果当前跳跃是往后跳,下次跳可以往前或后跳,位置 p - bp + a

如果都无法到达,返回 -1

解题

/**
 * @param {number[]} forbidden
 * @param {number} a
 * @param {number} b
 * @param {number} x
 * @return {number}
 */
var minimumJumps = function (forbidden, a, b, x) {
  const forbiddenSet = new Set(forbidden);
  if (forbiddenSet.has(a)) return -1;

  const low = 0;
  const upper = Math.max(Math.max(...forbidden) + a, x) + b;
  let jumps = [[0, 1, 0]];
  const visited = new Set([0]);
  while (jumps.length > 0) {
    const [position, direction, step] = jumps.shift();
    if (position === x) {
      return step;
    }
    let nextPosition = position + a;
    if (
      nextPosition <= upper &&
      !visited.has(nextPosition) &&
      !forbiddenSet.has(nextPosition)
    ) {
      visited.add(nextPosition);
      jumps.push([nextPosition, 1, step + 1]);
    }
    if (direction === 1) {
      nextPosition = position - b;
      if (
        nextPosition > low &&
        !visited.has(-nextPosition) &&
        !forbiddenSet.has(nextPosition)
      ) {
        visited.add(-nextPosition);
        jumps.push([nextPosition, -1, step + 1]);
      }
    }
  }

  return -1;
};