-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cycle finding algorithm and shortest trips implementation
- Loading branch information
1 parent
bbc96f2
commit 2b2ae78
Showing
12 changed files
with
219 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class TreeNode: | ||
def __init__(self, val=0, left=None, right=None): | ||
self.val = val | ||
self.left = left | ||
self.right = right | ||
|
||
def inorder(root): | ||
left = inorder(root.left) if root else [] | ||
root_ = [root.val] if root else [] | ||
right = inorder(root.right) if root else [] | ||
|
||
return left + root_ + right | ||
|
||
one = TreeNode(1) | ||
two = TreeNode(2) | ||
three = TreeNode(3) | ||
|
||
one.right = two | ||
two.left = three | ||
|
||
print(inorder(one)) | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
def coinChange(coins, amount): | ||
def dp(coins, amount): | ||
if amount == 0: | ||
return 0 | ||
|
||
if amount < 0: | ||
return float('inf') | ||
|
||
min_coins = float('inf') | ||
for coin in coins: | ||
n_coins = 1 + dp(coins, amount-coin) | ||
min_coins = min(min_coins, n_coins) | ||
|
||
return min_coins | ||
|
||
ans = dp(coins, amount) | ||
if ans == float('inf'): | ||
return -1 | ||
return ans | ||
|
||
print(coinChange([2, 1, 3], 4)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
def solveNQueens(n): | ||
board = [["."]*n for i in range(n)] | ||
|
||
res = [] | ||
def backtrack(current_row): | ||
if current_row == n: | ||
copy = ["".join(row) for row in board] | ||
res.append(copy) | ||
return | ||
|
||
|
||
for current_col in range(n): | ||
legal = True | ||
for previous_row in range(current_row): #iterate through the previous rows | ||
column_of_previous_queen = board[previous_row].index("Q") | ||
if column_of_previous_queen == current_col: | ||
legal = False | ||
# check diagonal above to the right | ||
if column_of_previous_queen == current_col + current_row - previous_row: | ||
legal = False | ||
|
||
if column_of_previous_queen == current_col - current_row + previous_row: | ||
legal = False | ||
|
||
if legal: | ||
board[current_row][current_col] = "Q" | ||
print(board) | ||
backtrack(current_row+1) | ||
board[current_row][current_col] = "." | ||
|
||
backtrack(0) | ||
return res | ||
|
||
print(solveNQueens(4)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
def subsets(elements): | ||
if len(elements) == 0: | ||
return [[]] | ||
|
||
first = elements[0] | ||
remaining_elements = elements[1:] | ||
subsets_without_first = subsets(remaining_elements) | ||
|
||
subsets_with_first = [] | ||
for sub in subsets_without_first: | ||
subsets_with_first.append([first, *sub]) | ||
|
||
return subsets_without_first + subsets_with_first | ||
|
||
|
||
print(subsets(["a", "b", "c"])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from collections import deque | ||
def numIslands(grid) -> int: | ||
q = deque([]) | ||
visited = set() | ||
count = 0 | ||
|
||
for row in range(len(grid)): | ||
for col in range(len(grid[0])): | ||
cell = (row, col) | ||
if cell not in visited and grid[row][col] == "1": | ||
q.appendleft(cell) | ||
count+=1 | ||
while q: | ||
currentCell = q.pop() | ||
visited.add(currentCell) | ||
row = currentCell[0] | ||
col = currentCell[1] | ||
#check down | ||
if row + 1 < len(grid) and grid[row+1][col] == "1": | ||
if (row+1, col) not in visited: | ||
q.appendleft((row+1,col)) | ||
#check up | ||
if row - 1 >= 0 and grid[row-1][col] == "1": | ||
if (row-1, col) not in visited: | ||
q.appendleft((row-1,col)) | ||
#check left | ||
if col - 1 >= 0 and grid[row][col-1] == "1": | ||
if (row, col-1) not in visited: | ||
q.appendleft((row,col-1)) | ||
#check right | ||
if col + 1 < len(grid[0]) and grid[row][col+1] == "1": | ||
if (row, col+1) not in visited: | ||
q.appendleft((row,col+1)) | ||
|
||
return count | ||
|
||
numIslands([["1", "1", "1", ], ["0", "1", "0"], ["1", "1", "1"]]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
def peasantMultiply(x, y): | ||
if x == 0: | ||
return 0 | ||
x_prime = x // 2 | ||
y_prime = y + y | ||
prod = peasantMultiply(x_prime, y_prime) | ||
if x % 2 == 1: | ||
prod += y | ||
return prod | ||
|
||
|
||
peasantMultiply(2, 3) |