Skip to content

Instantly share code, notes, and snippets.

View hongtaoh's full-sized avatar

Hongtao Hao hongtaoh

View GitHub Profile
@hongtaoh
hongtaoh / soln_lc79.py
Created October 7, 2024 19:42
Solution to Leetcode 79 Word Search
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
path = set()
def dfs(r, c, i):
if i == len(word):
return True
if (r<0 or c<0 or r>= ROWS or c>= COLS
or board[r][c] != word[i] or
@hongtaoh
hongtaoh / soln_lc20.py
Created October 7, 2024 00:22
Solution to Leetcode 20 Valid Parentheses
class Solution:
def isValid(self, s: str) -> bool:
dic = {"(":")", "{":"}", "[":"]"}
stack = []
# we know all char in s is either key or value in dic
for i in s:
# if key, store in stack
if i in dic:
stack.append(i)
# if value, check whether there is corresponding key
@hongtaoh
hongtaoh / soln_lc200.py
Created October 6, 2024 19:10
Solution to Leetcode 200 Number of Islands
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
# dfs, recurssion
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
result = 0
def dfs(r, c):
@hongtaoh
hongtaoh / soln_lc210.py
Created October 6, 2024 17:46
Solution to Leetcode 210. Course Schedule II
# BFS
class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
from collections import defaultdict, deque
graph = defaultdict(list)
for course, prereq in prerequisites:
graph[prereq].append(course)
# calculate in-degree
@hongtaoh
hongtaoh / us_state_abbrev.jl
Last active July 10, 2021 20:47
A Julia Dictionary to translate US States to Two letter codes and vice versa
# This Gist is based on https://gist.github.com/rogerallen/1583593 by Roger Allen
#
# United States of America Julia Dictionary to translate States,
# Districts & Territories to Two-Letter codes and vice versa.
#
# https://gist.github.com/hongtaoh/69b607623d39341466bca33ba7fd9ab0
#
# Dedicated to the public domain. To the extent possible under law,
# Hongtao Hao has waived all copyright and related or neighboring
# rights to this code.