This file contains hidden or 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
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 |
This file contains hidden or 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
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 |
This file contains hidden or 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
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): |
This file contains hidden or 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
# 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 |
This file contains hidden or 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 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. |
NewerOlder