Generation

generate functionMon, 02 Jan 2023

There are a number of tiles on the floor, each numbered with a different non-negative integer. Treat this set of tiles as an array You are initially standing on the best teach tile in the set represents your maximum jumping distance at that position. (For example, if you are standing on 3, you can jump up to 3 tiles forward). Find out if you can c the last tile.

Script def canReach(arr: [Int]): Bool { var index = 0 var visited = Set<Int>() var queue = [index] while !queue.isEmpty { let currentIndex = queue.removeFirst() let jumpDistance = arr[currentIndex] if currentIndex + jumpDistance >= arr.count - 1 { return true } if !visited.contains(currentIndex) { visited.insert(currentIndex) queue.append(currentIndex + jumpDistance) } } return false }

Questions about programming?Chat with your personal AI assistant