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 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 |
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 singly-linked list. | |
class ListNode(object): | |
def __init__(self, x): | |
self.val = x | |
self.next = None | |
class Solution(object): | |
def nextLargerNodes(self, head): |
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 | |
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 |
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 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] |
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 canReach(self, arr: List[int], start: int) -> bool: | |
stack = deque() | |
arr_length = len(arr) | |
stack.append(start) | |
available = [True] * arr_length | |
available[start] = False |
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 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) |
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 binary tree node. | |
class TreeNode: | |
def __init__(self, x): | |
self.val = x | |
self.left = None | |
self.right = None | |
class Solution: |
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 reverseWords(self, s: str) -> str: | |
return ' '.join(reversed(s.split())) |
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 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() |
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 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] |