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 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() |
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 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() |
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
# 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: |
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
# 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)} |
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 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() |
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
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:]): |
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
# 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): |
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
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 |
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
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 |
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 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 | |