We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Castle on the Grid
Castle on the Grid
Sort by
recency
|
329 Discussions
|
Please Login in order to post a comment
simply O(n^3) algo
Python easy implementation using deque:
import os from collections import deque
def minimumMoves(grid, startX, startY, goalX, goalY): # Define the directions for up, down, left, right movements directions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
if name == 'main': fptr = open(os.environ['OUTPUT_PATH'], 'w')
deque helps in improving time complexity to O(1) ... while append and pop the points/elements.
We don't necessarily need to build a graph. We can use a queue directly. Here is my Python solution: