1654. Minimum Jumps to Reach Home

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 1654

class Solution:
    def minimumJumps(self, forbidden: List[int], a: int, b: int, x: int) -> int:
        s = set(forbidden)
        q = deque([(0, 1)])
        vis = {(0, 1)}
        res = 0
        while q:
            for _ in range(len(q)):
                i, k = q.popleft()
                if i == x:
                    return res
                nxt = [(i + a, 1)]
                if k & 1:
                    nxt.append((i - b, 0))
                for j, k in nxt:
                    if 0 <= j < 6000 and j not in s and (j, k) not in vis:
                        q.append((j, k))
                        vis.add((j, k))
            res += 1
        return -1