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 kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]: | |
return sorted([[n1, n2] for n1 in nums1 for n2 in nums2], key=lambda x: x[0] + x[1])[:k] | |
# if not len(nums1) or not len(nums2): return [] | |
# ret = [] | |
# pointers = [0 for _ in nums1] |
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
import heapq | |
import numpy as np | |
class Solution: | |
def kthSmallest(self, matrix: List[List[int]], k: int) -> int: | |
one_matrix = np.asarray(matrix.copy()).flatten().tolist() | |
heapq.heapify(one_matrix) | |
for _ in range(k - 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 closedIsland(self, grid: List[List[int]]) -> int: | |
def appendNextIsland(x, y): | |
if not (0 < x < X - 1 and 0 < y < Y - 1): self.is_closed = False | |
for i, j in [(x + 1, y), (x, y + 1), (x - 1, y), (x, y - 1)]: | |
if not (0 <= i < X and 0 <= j < Y) or grid[i][j] == 1 or not available[i][j]: continue | |
stack.append([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
# Definition for a binary tree node. | |
class TreeNode: | |
def __init__(self, x): | |
self.val = x | |
self.left = None | |
self.right = None | |
class Solution: | |
def maxAncestorDiff(self, root: TreeNode) -> int: | |
self.ret = 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
from collections import deque | |
class Solution: | |
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]: | |
ret = [] | |
nums.sort() | |
stack = deque() | |
stack.append([[], 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: | |
def subsets(self, nums: List[int]) -> List[List[int]]: | |
ret = [] | |
stack = deque() | |
stack.append([[], nums]) | |
while stack: | |
subset, rest = stack.popleft() |
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 merge(self, intervals: List[List[int]]) -> List[List[int]]: | |
intervals.sort(key=lambda i: i[0]) | |
try: | |
ret = [intervals[0]] | |
except: | |
return [] | |
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 Counter | |
class Solution: | |
def customSortString(self, S: str, T: str) -> str: | |
ret = '' | |
ref = dict(Counter(list(T))) | |
for s in S: | |
if not s in ref: continue | |
ret += s * ref[s] |
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 numEnclaves(self, A: List[List[int]]) -> int: | |
def appendNeighbors(x, y): | |
if not (0 < x < X - 1 and 0 < y < Y - 1): self.is_enclaves = False | |
for i, j in [(x + 1, y), (x, y + 1), (x - 1, y), (x, y - 1)]: | |
if not (0 <= i < X and 0 <= j < Y) or A[i][j] == 0 or not available[i][j]: continue | |
stack.append([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
from collections import Counter | |
class Solution: | |
def frequencySort(self, s: str) -> str: | |
c = Counter(list(s)) | |
ret = '' | |
for spell, count in c.most_common(): | |
ret += spell * count | |
return ret |
NewerOlder