Skip to content

Instantly share code, notes, and snippets.

View nma's full-sized avatar

Nick Ma nma

View GitHub Profile
@nma
nma / combinations_iterative.py
Created April 8, 2019 01:17
Combinations Iterative
class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
nums = list(range(1, n + 1))
combinations = [[]]
for depth in range(0, k):
new_combinations = []
for combo in combinations:
start_index = nums.index(combo[-1]) + 1 if len(combo) > 0 else 0
@nma
nma / permutations_iterative.py
Created April 8, 2019 01:51
Permutations Iterative
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
permutations = [[]]
for choice in nums:
new_permutations = []
for perm in permutations:
perm.append(choice)
for j in range(len(perm)):
@nma
nma / buy_and_sell_stock_single_txn.py
Created June 22, 2019 19:13
Best time to buy and sell stock single transaction
class Solution:
def maxProfit(self, prices: List[int]) -> int:
if len(prices) < 2:
return 0
low_price = prices[0]
max_profit = 0
for i in range(1, len(prices)):
if prices[i] > low_price:
@nma
nma / erd_example.puml
Last active June 28, 2019 15:31 — forked from QuantumGhost/example.puml
A simple template for PlantUML to draw ER diagram.The basic idea comes from http://plantuml.sourceforge.net/qa/?qa=331/database-modeling
@startuml
' uncomment the line below if you're using computer with a retina display
' skinparam dpi 300
!define Table(name,desc) class name as "desc" << (T,#FFAAAA) >>
' we use bold for primary key
' green color for unique
' and underscore for not_null
!define primary_key(x) <b>x</b>
!define unique(x) <color:green>x</color>
!define not_null(x) <u>x</u>
from dataclasses import dataclass
@dataclass
class TraversalRecord:
seen_nodes: int
ancestor: BinaryTreeNode
def post_order_traversal_helper(node: BinaryTreeNode, n1: BinaryTreeNode, n2: BinaryTreeNode) -> TraversalRecord:
'''
@nma
nma / lca_parent_ptr.py
Last active March 15, 2020 19:55
Lowest Common Ancestor Algorithm with Parent Pointers
def find_lca(n1: BinaryTreeNode, n2: BinaryTreeNode) -> BinaryTreeNode:
def get_depth(node: BinaryTreeNode) -> int:
depth = 0
while node and node.parent:
node = node.parent
depth += 1
return depth
d1 = get_depth(n1)
d2 = get_depth(n2)
@nma
nma / lca_bst.py
Last active March 23, 2020 14:46
LCA algorithm in a BST
# n1.data <= n2.data
def find_lca(tree: BstNode, n1: BstNode, n2: BstNode) -> Optional[BstNode]:
while tree:
# both left and right child are greater than current node
if n1.data > tree.data and n2.data > tree.data:
tree = tree.right
# both left and right child are less than the current node
elif n1.data < tree.data and n2.data < tree.data:
tree = tree.left
@nma
nma / lca_parent_ptr_set.py
Created March 15, 2020 19:59
LCA algorithm with a parent pointer and a set to save time
def find_lca(n1: BinaryTreeNode, n2: BinaryTreeNode) -> Optional[BinaryTreeNode]:
seen_on_path: Set[BinaryTreeNode] = set()
while n1 or n2:
# go up the tree at the same time
if n1:
# at every point check if we have already seen the node
if n1 in seen_on_path:
# first seen will be an ancestor
def rob_houses(house: Tuple[int]) -> int:
if len(house) == 0: return 0
if len(house) == 1: return house[0]
criminal_gains = max(
rob_houses(house[1:]),
house[1] + rob_houses(house[2:])
)
return criminal_gains
def rob_houses_mem(house: Tuple[int], mem: Dict[int, int]) -> int:
if len(house) == 0: return 0
if hash(house) in mem:
return mem[hash(house)]
criminal_gains = max(
rob_houses(house[1:], mem),
house[1] + rob_houses(house[2:], mem)
)