Skip to content

Instantly share code, notes, and snippets.

View liketheflower's full-sized avatar

jimmy shen liketheflower

View GitHub Profile
class Solution:
def numWays(self, steps: int, arrLen: int) -> int:
MOD = 10**9 + 7
POS = min(steps+1, arrLen)
dp = [[0 for step in range(steps+1)] for pos in range(POS)]
def bfs(pos, step):
cur_level = {(pos, step)}
dp[pos][step] = 1
while cur_level:
nxt_level = set()
class Solution:
def numWays(self, steps: int, arrLen: int) -> int:
MOD = 10**9 + 7
POS = min(steps//2+1, arrLen)
dp = [[0 for step in range(steps+1)] for pos in range(POS)]
def bfs(pos, step):
cur_level = {(pos, step)}
dp[pos][step] = 1
while cur_level:
nxt_level = set()
# naive BFS will get TLE
class Solution:
def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, K: int) -> int:
graph = collections.defaultdict(list)
for a, b, c in flights:
graph[a].append((b,c))
res = []
def bfs(city):
cur_level = {(city, 0, 0)}
while cur_level:
# prune the branches which has larger total cost than the current min_price
class Solution:
def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, K: int) -> int:
graph = collections.defaultdict(list)
for a, b, c in flights:
graph[a].append((b,c))
def bfs(city):
min_price = float('inf')
cur_level = {(city, 0, 0)}
class Solution:
def numWays(self, steps: int, arrLen: int) -> int:
MOD = 10**9 + 7
POS = min(steps//2+1, arrLen)
dp = [[0 for step in range(steps+1)] for pos in range(POS)]
def bfs(pos, step):
cur_level = {(pos, step)}
dp[pos][step] = 1
while cur_level:
nxt_level = set()
from functools import lru_cache
class Solution:
def shortestSuperstring(self, A: List[str]) -> str:
# TSP, DP
n = len(A)
w = [[0] * n for _ in range(n)]
for i in range(n):
for j in range(n):
for k in range(min(len(A[i]), len(A[j])), 0, -1):
if A[j].startswith(A[i][-k:]):
# Enter your code here. Read input from STDIN. Print output to STDOUT
N = int(input())
raw_src = [input() for _ in range(N)]
raw_des = [input() for _ in range(N)]
src, des = "", ""
for i in range(N):
src += raw_src[i]
des += raw_des[i]
from collections import deque
class Sliders(object):
from collections import deque
class Solution:
def numWays(self, steps: int, arrLen: int) -> int:
MOD = 10**9 + 7
POS = min(steps//2+1, arrLen)
dp = [[0 for step in range(steps+1)] for pos in range(POS)]
def bfs(pos, step):
queue = deque([(pos, step)])
dp[pos][step] = 1
from collections import deque
class Solution:
def minPushBox(self, grid: List[List[str]]) -> int:
R, C = len(grid), len(grid[0])
for i in range(R):
for j in range(C):
if grid[i][j] == 'S':
pi, pj = i, j
elif grid[i][j] == 'B':
bi, bj = i, j
class Solution:
def minPushBox(self, grid: List[List[str]]) -> int:
R, C = len(grid), len(grid[0])
for i in range(R):
for j in range(C):
if grid[i][j] == 'S':
pi, pj = i, j
elif grid[i][j] == 'B':
bi, bj = i, j