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 uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int: | |
X, Y = len(obstacleGrid), len(obstacleGrid[0]) | |
dp = [[0 for _ in range(Y)] for _ in range(X)] | |
for x in range(X): | |
for y in range(Y): | |
if obstacleGrid[x][y] == 1: continue | |
if 0 < x and 0 < y: | |
dp[x][y] += sum([dp[x - 1][y], dp[x][y - 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
class Solution: | |
def uniquePaths(self, m: int, n: int) -> int: | |
dp = [[0 for _ in range(m)] for _ in range(n)] | |
for x in range(n): | |
for y in range(m): | |
if 0 < x and 0 < y: | |
dp[x][y] += sum([dp[x - 1][y], dp[x][y - 1]]) | |
elif x == 0 and 0 < y: | |
dp[x][y] += dp[x][y - 1] | |
elif 0 < x and y == 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 minPathSum(self, grid: List[List[int]]) -> int: | |
dp = grid.copy() | |
X, Y = len(grid), len(grid[0]) | |
for x in range(X): | |
for y in range(Y): | |
if 0 < x and 0 < y: | |
dp[x][y] += min([dp[x - 1][y], dp[x][y - 1]]) | |
elif x == 0 and y != 0: | |
dp[x][y] += dp[x][y - 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 bisect import bisect_left | |
# Definition for singly-linked list. | |
class ListNode: | |
def __init__(self, x): | |
self.val = x | |
self.next = None | |
class Solution: | |
def numComponents(self, head: ListNode, G: List[int]) -> int: | |
sg = sorted(G.copy()) |
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 singleNumber(self, nums: List[int]) -> List[int]: | |
snums = sorted(nums.copy()) | |
idx = 0 | |
ret = [] | |
while idx + 1 < len(snums): | |
if snums[idx] == snums[idx + 1]: | |
idx += 2 | |
continue | |
ret.append(snums[idx]) |
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 singleNumber(self, nums: List[int]) -> int: | |
snums = sorted(nums.copy()) | |
idx = 0 | |
while idx + 2 < len(snums): | |
if snums[idx] == snums[idx + 1] and snums[idx] == snums[idx + 2]: | |
idx += 3 | |
else: | |
return snums[idx] | |
return snums[-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 scoreOfParentheses(self, S: str) -> int: | |
stack = deque() | |
tail_idx = 0 | |
ret = 0 | |
for head_idx, s in enumerate(S): | |
if s == '(': | |
stack.append(s) | |
continue |
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 findCircleNum(self, M: List[List[int]]) -> int: | |
# union-find | |
def find(i): | |
if parent[i] != i: | |
parent[i] = find(parent[i]) | |
return parent[i] | |
def union(x, y): |
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 countArrangement(self, N: int) -> int: | |
ret = 0 | |
pointer = 1 | |
nums = [n for n in range(1, N + 1)] | |
stack = deque([[ pointer, nums[:idx] + nums[idx + 1:] if idx < N else nums[:idx] ] for idx, n in enumerate(nums)]) | |
while stack: | |
pointer, rest = stack.pop() |
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 removeDuplicates(self, s: str, k: int) -> str: | |
stack = deque() | |
conseq = [] | |
last_used = '' | |
for spell in s: | |
stack.append(spell) | |
if last_used == '' or last_used != spell: |