Leetcode1306. Jumping Game III(medium,BFS,DFS)

content

1. Problem description

2. Problem solving analysis

3. Code implementation


1. Problem description

Here is an array of non-negative integers  arr, and you start at the starting index  start of the array. When you are at the subscript  i , you can skip to  i + arr[i] or  i - arr[i].

Please judge whether you can jump to   any subscript whose corresponding element value is 0.

Note that you cannot jump outside the array under any circumstances.

Example 1:

Input: arr = [4,2,3,0,3,1,2], start = 5
 Output: true
 Explanation:
There are the following possible scenarios for reaching index 3 with value 0:
subscript 5 -> subscript 4 -> subscript 1 -> subscript 3
Subscript 5 -> Subscript 6 -> Subscript 4 -> Subscript 1 -> Subscript 3

Example 2:

Input: arr = [4,2,3,0,3,1,2], start = 0
 Output: true 
 Explanation:
 reaching subscript 3 with value 0 has the following possible solutions:
subscript 0 -> subscript 4 -> subscript 1 -> subscript 3

Example 3:

Input: arr = [3,0,2,1,2], start = 2
 Output: false
 Explanation: Unable to reach subscript 1 with value 0.

hint:

  • 1 <= arr.length <= 5 * 10^4
  • 0 <= arr[i] < arr.length
  • 0 <= start < arr.length

Source: LeetCode
Link: https://leetcode-cn.com/problems/jump-game-iii The
copyright belongs to LeetCode.com. For commercial reprints, please contact the official authorization, and for non-commercial reprints, please indicate the source.

2. Problem solving analysis

        It should be a common depth or breadth-first search.

         At each node, you can jump left or right, but you must not jump out (that is, out of bounds), and do not repeat visits (repeated visits are meaningless).

3. Code implementation

from typing import List
from collections import deque
class Solution:
    def canReach(self, arr: List[int], start: int) -> bool:
        
        q = deque([start])
        visited = set([start])
        while len(q) > 0:
            node = q.pop()
            if arr[node] == 0:
                return True
            left = node - arr[node]
            if left >= 0 and left not in visited:
                q.append(left)
                visited.add(left)
            
            right = node + arr[node]
            if right < len(arr) and right not in visited:
                q.append(right)
                visited.add(right)

        return False
    
if __name__ == "__main__":
    
    sln = Solution()
    
    arr = [4,2,3,0,3,1,2]
    start = 5
    print(sln.canReach(arr, start))
    
    arr = [4,2,3,0,3,1,2]
    start = 0
    print(sln.canReach(arr, start))
    
    arr = [3,0,2,1,2]
    start = 2
    print(sln.canReach(arr, start))
    
    arr = [0,1]
    start = 1
    print(sln.canReach(arr, start))

        Execution time: 52 ms, beat 94.80% of users in all Python3 commits

        Memory consumption: 20.3 MB, beats 45.97% of users across all Python3 commits

        Well, just write it casually, and you can be satisfied with this performance.

        Back to the main directory: Leetcode daily problem-solving notes (dynamic update...) 

Guess you like

Origin blog.csdn.net/chenxy_bwave/article/details/124240654