Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions n-queens.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Run a loop through the colums (0 to col). If the queen can be placed in the specific
# index, of the row and col, make board[row][col] as true and then recursively call the helper
# function for row=row+1.then backtrack making board[row][col] as false. if the row == n, print
# and append the result

# time: O(n!)
# space: O(n)

class Solution(object):
def solveNQueens(self, n):
"""
:type n: int
:rtype: List[List[str]]
"""
self.result=[]
self.board = [[None for j in range(n)]for i in range(n)]
self.helper(n,self.board,0)
return self.result


def helper(self, n, board,r):
if r==n:
l=[]

for i in range(n):
st=''
for j in range(n):
if board[i][j]==True:
st=st+'Q'
else:
st=st+'.'
l.append(st)
self.result.append(l)
return True


for c in range(n):
if self.isSafe(n,board,r,c):
board[r][c]=True
self.helper(n, board, r+1)
board[r][c]=False

def isSafe(self, n, board, r, c):
#top
for i in range(r):
if board[i][c]==True:
return False
#top left
i=r-1; j=c-1
while i>=0 and j>=0:
if board[i][j]==True:
return False
i-=1
j-=1
#top right
i=r-1; j=c+1
while i>=0 and j<n:
if board[i][j]==True:
return False
i-=1
j+=1
return True

54 changes: 54 additions & 0 deletions wordsearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

# run i from 0 to rows and j from 0 to cols and check if the element at board[i][j] is the
# char at word[0]. If it is, call the recursive function backtrack() and return true if the
# recursive function returns true. By default return false.

# Recursive function:
# Check if the ind reached len of word. If it does, return true. If i or j is out of bounds, return
# false. If the element at board(i,j)==word[index], change the eleemt there to a const and
# iteratr through 4 directions. In each direction call backyracking with index+1 and return True
# if the call returns true. Then remove the const from the board by replacing it with the element
# at word(ind). By default return false.

# Time O(m*n)*3^

# Space O(n)

class Solution(object):

def backtrack(self,board,i,j,ind,word):

if ind==len(word):
return True
if i<0 or j<0 or i==len(board) or j ==len(board[0]):
return False

if board[i][j]==word[ind]:
board[i][j]='*'
for d in self.direc:
nr=i+d[0]
nc=j+d[1]
if self.backtrack(board,nr,nc,ind+1,word):
return True
board[i][j]=word[ind]
return False


def exist(self, board, word):
"""
:type board: List[List[str]]
:type word: str
:rtype: bool
"""
self.direc=[[0,1],[1,0],[-1,0],[0,-1]]
m=len(board)
n=len(board[0])
for i in range(m):
for j in range(n):
if board[i][j]==word[0]:
if self.backtrack(board,i,j,0,word):
return True

return False