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 minFallingPathSum(self, A: List[List[int]]) -> int: | |
H, W = len(A), len(A[0]) | |
dp = A.copy() | |
for x in range(1, H): | |
for y in range(W): | |
if y == 0: | |
tmp = [dp[x - 1][y], dp[x - 1][y + 1]] | |
elif y == W - 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 numberOfArithmeticSlices(self, A: List[int]) -> int: | |
ret = 0 | |
length = len(A) | |
if length == 0: return ret | |
dp = [1] * length | |
for idx in range(1, length): | |
dp[idx] = 2 if idx == 1 or A[idx] - A[idx - 1] != A[idx - 1] - A[idx - 2] else dp[idx - 1] + 1 | |
if 3 <= dp[idx]: ret += dp[idx - 1] - 1 | |
return ret |
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(object): | |
def mincostTickets(self, days, costs): | |
""" | |
:type days: List[int] | |
:type costs: List[int] | |
:rtype: int | |
""" | |
dp = [0] * (days[-1] + 1) | |
day_idx = 0 | |
for cur_day in range(1, days[-1] + 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(object): | |
def validateStackSequences(self, pushed, popped): | |
""" | |
:type pushed: List[int] | |
:type popped: List[int] | |
:rtype: bool | |
""" | |
# smart answer |
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(object): | |
def permute(self, nums): | |
""" | |
:type nums: List[int] | |
:rtype: List[List[int]] | |
""" | |
ret = [] | |
stack = deque([[[],nums]]) | |
while stack: |
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(object): | |
def dailyTemperatures(self, T): | |
""" | |
:type T: List[int] | |
:rtype: List[int] | |
""" | |
last_idx = len(T) - 1 | |
ret = [0] * (last_idx + 1) | |
stack = deque() |
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(object): | |
def topKFrequent(self, nums, k): | |
""" | |
:type nums: List[int] | |
:type k: int | |
:rtype: List[int] | |
""" | |
hashed_counters = {} | |
for n in nums: | |
hashed_counters[n] = hashed_counters.get(n, 0) + 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(object): | |
def singleNonDuplicate(self, nums): | |
""" | |
:type nums: List[int] | |
:rtype: int | |
""" | |
idx = 0 | |
last_idx = len(nums) - 1 | |
while True: | |
if idx == last_idx or nums[idx] != nums[idx + 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 | |
""" | |
# Definition for a Node. | |
class Node(object): | |
def __init__(self, val=None, children=None): | |
self.val = val | |
self.children = children | |
""" | |
class Solution(object): | |
def levelOrder(self, root): |
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
# Definition for a binary tree node. | |
class TreeNode(object): | |
def __init__(self, x): | |
self.val = x | |
self.left = None | |
self.right = None | |
class Solution(object): | |
memo = {0: [], 1: [TreeNode(0)]} | |
def allPossibleFBT(self, N): |