Skip to content

Instantly share code, notes, and snippets.

@ryuji0123
ryuji0123 / LeetCode 147
Created January 10, 2020 06:30
LeetCode 147. Insertion Sort List
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Solution(object):
def insertionSortList(self, head):
"""
:type head: ListNode
@ryuji0123
ryuji0123 / LeetCode 1019
Last active January 10, 2020 05:51
LeetCode 1019. Next Greater Node In Linked List
from collections import deque
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Solution(object):
def nextLargerNodes(self, head):
@ryuji0123
ryuji0123 / LeetCode 215
Created January 9, 2020 02:13
LeetCode 215. Kth Largest Element in an Array
import heapq
class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
hnums = list(map(lambda x: x * -1, nums))
heapq.heapify(hnums)
for _ in range(k - 1): heapq.heappop(hnums)
return heapq.heappop(hnums) * -1
@ryuji0123
ryuji0123 / LeetCode 75
Created January 9, 2020 02:04
LeetCode 75. Sort Colors
class Solution:
def sortColors(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
counter = [0] * 3
for n in nums:
counter[n] += 1
nums[:counter[0]] = [0] * counter[0]
nums[counter[0]: counter[1]] = [1] * counter[1]
@ryuji0123
ryuji0123 / LeetCode 1306
Created January 8, 2020 10:29
LeetCode 1306. Jump Game III
from collections import deque
class Solution:
def canReach(self, arr: List[int], start: int) -> bool:
stack = deque()
arr_length = len(arr)
stack.append(start)
available = [True] * arr_length
available[start] = False
@ryuji0123
ryuji0123 / LeetCode 55
Last active January 8, 2020 10:03
LeetCode 55. Jump Game
class Solution:
def canJump(self, nums: List[int]) -> bool:
head_idx = 0
num_length = len(nums)
for tail_idx in range(num_length):
if num_length - 1 <= head_idx: return True
if head_idx < tail_idx: return False
head_idx = max(tail_idx + nums[tail_idx], head_idx)
@ryuji0123
ryuji0123 / LeetCode 1302
Created January 8, 2020 06:15
LeetCode 1302. Deepest Leaves Sum
from collections import deque
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
@ryuji0123
ryuji0123 / LeetCode 151
Created January 8, 2020 06:00
LeetCode 151. Reverse Words in a String
class Solution:
def reverseWords(self, s: str) -> str:
return ' '.join(reversed(s.split()))
@ryuji0123
ryuji0123 / LeetCode 49
Created January 8, 2020 05:49
LeetCode 49. Group Anagrams
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
ref = {}
for s in strs:
sorted_s = ''.join(sorted(s))
if sorted_s not in ref:
ref[sorted_s] = [s]
else:
ref[sorted_s].append(s)
return ref.values()
@ryuji0123
ryuji0123 / LeetCode 96
Created January 8, 2020 05:26
LeetCode 96. Unique Binary Search Trees
from collections import deque
class Solution:
def numTrees(self, n: int) -> int:
dp = [0] * (n + 1)
dp[0] = 1
for i in range(1, n + 1):
for j in range(0, i):
dp[i] += dp[j] * dp[i - j - 1]
return dp[n]