Skip to content

Instantly share code, notes, and snippets.

@ryuji0123
ryuji0123 / LeetCode 373
Created February 8, 2020 07:55
LeetCode 373. Find K Pairs with Smallest Sums
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]
@ryuji0123
ryuji0123 / LeetCode 378
Created February 8, 2020 07:01
LeetCode 378. Kth Smallest Element in a Sorted Matrix
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):
@ryuji0123
ryuji0123 / LeetCode 1254
Created January 15, 2020 06:54
LeetCode 1254. Number of Closed Islands
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])
@ryuji0123
ryuji0123 / LeetCode 1026
Created January 15, 2020 06:41
LeetCode 1026. Maximum Difference Between Node and Ancestor
# 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
@ryuji0123
ryuji0123 / LeetCode 90
Last active January 14, 2020 08:09
LeetCode 90. Subsets II
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:
@ryuji0123
ryuji0123 / LeetCode 78
Last active January 14, 2020 08:01
LeetCode 78. Subsets
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()
@ryuji0123
ryuji0123 / LeetCode 56
Created January 14, 2020 07:41
LeetCode 56. Merge Intervals
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
@ryuji0123
ryuji0123 / LeetCode 791
Created January 14, 2020 07:00
LeetCode 791. Custom Sort String
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]
@ryuji0123
ryuji0123 / LeetCode 1020
Last active January 14, 2020 05:54
LeetCode 1020. Number of Enclaves
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])
@ryuji0123
ryuji0123 / LeetCode 451
Created January 14, 2020 05:04
LeetCode 451. Sort Characters By Frequency
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