Skip to content

Instantly share code, notes, and snippets.

View hongtaoh's full-sized avatar

Hongtao Hao hongtaoh

View GitHub Profile
@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.
@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 / 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_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_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_lc394.py
Created October 7, 2024 21:39
Solution to Leetcode 394 Decode String
class Solution:
def decodeString(self, s: str) -> str:
stack = []
for i in s:
if i != "]":
stack.append(i)
else:
substring = ""
while stack[-1] != "[":
@hongtaoh
hongtaoh / soln_lc3.py
Created October 8, 2024 03:04
Solution to Leetcode 3 Longest Substring Without Repeating Characters
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
charSet = set()
left = 0
maxLen = 0
for right in range(len(s)):
while s[right] in charSet:
charSet.remove(s[left])
left += 1
charSet.add(s[right])
@hongtaoh
hongtaoh / soln_lc1071.py
Created October 9, 2024 15:03
Solution to Leetcode 1071 Greatest Common Divisor of Strings
class Solution:
# the most straightforward implementation
def gcdOfStrings(self, str1: str, str2: str) -> str:
short_str, long_str = (str1, str2) if len(str1) <= len(str2) else (str2, str1)
l = 0
while l < len(short_str):
r = len(short_str)
while r >l and r <= len(short_str):
substr = short_str[l:r]
substr_len = r - l
@hongtaoh
hongtaoh / soln_lc70.py
Created October 9, 2024 16:10
Solution to Leetcode 70 Climbing Stairs
class Solution:
# dynamic programming
def climbStairs(self, n: int) -> int:
one, two = 1, 1
for _ in range(n-1):
temp = one
one += two
two = temp
return one
@hongtaoh
hongtaoh / soln_lc34.py
Created October 9, 2024 22:20
Solution to Leetcode 34 Find First and Last Position of Element in Sorted Array
class Solution:
def searchRange(self, nums: List[int], target: int) -> List[int]:
left = self.binSearch(nums, target, True)
right = self.binSearch(nums, target, False)
return [left, right]
# modified binary search algorithm
def binSearch(self, nums, target, leftBiased):
l = 0
r = len(nums) - 1