This is a Leetcode problem -
On an \$N\$ x \$N\$
board
, the numbers from1
toN * N
are written boustrophedonically (starting from the bottom left of the board), and alternating direction each row. For example, for a 6 x 6 board, the numbers are written as follows -You start on square
1
of the board (which is always in the last row and first column). Each move, starting from squarex
, consists of the following -
- You choose a destination square
S
with numberx+1, x+2, x+3, x+4, x+5
, orx+6
, provided this number is<= N * N
.
- (This choice simulates the result of a standard 6-sided die roll, ie., there are always at most 6 destinations, regardless of the size of the board.)
- If
S
has a snake or ladder, you move to the destination of that snake or ladder. Otherwise, you move toS
.A board square on row
r
and columnc
has a "snake or ladder" ifboard[r][c] != -1
. The destination of that snake or ladder isboard[r][c]
.Note that you only take a snake or ladder at most once per move; if the destination to a snake or ladder is the start of another snake or ladder, you do not continue moving. (For example, if the board is
[[4,-1],[-1,3]]
, and on the first move your destination square is2
, then you finish your first move at3
because you do not continue moving to4
.)Return the least number of moves required to reach square
N * N
. If it is not possible, return-1
.Note -
2 <= board.length = board[0].length <= 20
board[i][j]
is between1
andN * N
or is equal to-1
.- The board square with number
1
has no snake or ladder.- The board square with number
N * N
has no snake or ladder.Example 1 -
Input: [ [-1,-1,-1,-1,-1,-1], [-1,-1,-1,-1,-1,-1], [-1,-1,-1,-1,-1,-1], [-1,35,-1,-1,13,-1], [-1,-1,-1,-1,-1,-1], [-1,15,-1,-1,-1,-1]] Output: 4 """ Explanation - At the beginning, you start at square 1 [at row 5, column 0]. You decide to move to square 2, and must take the ladder to square 15. You then decide to move to square 17 (row 3, column 5), and must take the snake to square 13. You then decide to move to square 14, and must take the ladder to square 35. You then decide to move to square 36, ending the game. It can be shown that you need at least 4 moves to reach the N*N-th square, so the answer is 4. """
I would like to have a performance review of my solution and would also like to know whether I could make it more efficient.
Here is my solution to this challenge (in Python 3) -
# Uses Breadth First Search (BFS)
def snakes_and_ladders(board):
"""
:type board: List[List[int]]
:rtype: int
"""
board_2 = [0]
rows, cols = len(board), len(board[0])
row = rows - 1
while row >= 0:
for col in range(cols):
board_2.append(board[row][col])
row -= 1
if row >= 0:
for col in range(cols - 1, -1, -1):
board_2.append(board[row][col])
row -= 1
visited = [0 for i in range(len(board_2))]
stack = collections.deque()
stack.append([1,0])
while stack:
current_index, current_dist = stack.popleft()
for i in range(1,7):
next_index = min(rows * cols, current_index + i)
if board_2[next_index] != -1:
next_index = board_2[next_index]
if next_index == rows * cols:
return current_dist + 1
if visited[next_index] == 0:
visited[next_index] = 1
stack.append([next_index, current_dist + 1])
return -1
1 Answer 1
Type Hinting
You were halfway there! Good job having the type of values accepted and returned in the method docstring. Now, you can use type hints to show in the header of the method what values are accepted and returned, as follows
from typing import List
def snakes_and_ladders(board: List[List[int]]) -> int:
Consistent Spacing
This
for i in range(1,7):
should be this
for i in range(1, 7):
You have good spacing in the rest of your code, but you should stay consistent and apply this spacing everywhere.
The same for this line
stack.append([1,0]) -> stack.append([1, 0])
Magic Numbers
We're coming back to this line again
for i in range(1, 7):
What is 7
supposed to represent? The max number of rows or columns? What if you have to change it later on to apply to smaller/bigger snakes and ladders boards? I would advise using a variable to hold this value, naming it accordingly.
List Comprehension
You use lots of loops with one line in them. Particularly when appending to lists. Luckily, you can add two lists together and it will merge them.
From this
for col in range(cols):
board_2.append(board[row][col])
to this
board_2 += [board[row][col] for col in range(cols)]
You can do the same for the next loop a couple lines down
From this
for col in range(cols - 1, -1, -1):
board_2.append(board[row][col])
to this
board_2 += [board[row][col] for col in range(cols - 1, -1, -1)]
Explore related questions
See similar questions with these tags.
sanify(board)
. \$\endgroup\$