Skip to content

Instantly share code, notes, and snippets.

Avatar

Ray liyunrui

View GitHub Profile
View 295. Find Median from Data Stream.py
class MedianFinder:
"""
Problem Clarification
If it is a steam (dynamically update/delete/add array)?
-> Yes
Thought Process
Brute Force:
step1: maintain a array by each time we called addnum, we sorted again. ->O(nlogn)
or
View insertionSort.py
class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
return self.insertionSort(nums)
def insertionSort(self, A):
"""
[5,2,3,1]
i
j
View 272. Closest Binary Search Tree Value II.py
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
"""
Problem Clarifiaction:
the answer we return, order dose not matter. For example, [3,4] and [4,3] both are accepted.
View 236. Lowest Common Ancestor of a Binary Tree.py
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
"""
Thought Process:
View 297. Serialize and Deserialize Binary Tree.py
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Codec:
"""
Thought Process:
View 199. Binary Tree Right Side View.py
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
"""
Thought Process:
View 103. Binary Tree Zigzag Level Order Traversal.py
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
"""
Thought Process
View 337. House Robber III.py
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
"""
Problem Clarification
-We must ask interviewer to go through the examples.
@liyunrui
liyunrui / countSort.py
Last active Nov 30, 2020
leetcode-sorting
View countSort.py
class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
return self.countSort(nums)
def countSort(self, A):
"""
It's integer sorting
T: O(n+k), n is nb of data in nums and k is range between in min and max.
S: O(c) in the way we implemented( c is nb of unique elements)
Note:
View bucketSort.py
class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
return self.bucketSort(nums)
def bucketSort(self, A):
"""
1. It's in-place sorting.
2. Bucket sort is mainly useful when input is uniformly distributed over a range.(floating number is allowed)
step1: create n buckets
step2: put element into buckets
You can’t perform that action at this time.