LC-Meta Flashcards
Binary tree vertical order traversal
You know it
Add tuple to dictionary;
Pop left from queue
Valid word abbreviation
- Make sure index2 is in range while getting the count
- Check if enough remaining chars in word and early return
Nested list weight sum
Be careful about updating primitive variables with inner function.
lowest common ancestor iii - with parent pointer
Two pointers that can merge, making sure they go through the same distance and eventually merge
lowest common ancestor ii - nodes may or may not exist
Post order
Return (none, 0) as default after checking left and right
Minimum remove to make valid parentheses - return just one valid string
Two pass; maintain balance.
T - N
S - N
Valid Palindrome I
Get lower case of letter lower()
Check if letter is alphanumeric isalnum()
Reverse a string or list
Valid palindrome II - remove at most one letter
Start from head and tail; whenever no match found, try remove either side
Dot product of two sparse vectors
- Get both index and element by using enumerate func
- Get both key and value by items func
- Consider using array instead of hashmap!
Validate palindrome - remove at most K letters
Think about base cases
Memorization
How to infer current from child cases
Random pick with weight
Prefix sum the same length as array
Binary search - if low < target; else; then check both low and high
Random function - random.randint(start, stop) both inclusive
Basic calculator II
- Track current operation
- How to truncate towards 0 when doing division for negative values
- isdigit()
- isspace()
Basic calculator - with parentheses and only addition and subtraction
- In place update result, we still need a stack though - why?
- When operator found, finish previous calculation and reset the sign and operand
- When open parenthesis found, cache the current sign and result in stack. Reset the result and sign
- When closing parenthesis found, finish previous calculation first, pop from stack and complete the calculation outside of the parenthesis. Reset operator.
Lowest common ancestor - two nodes must exist
Do preorder and early return
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/
Lowest common ancestor of binary search tree
Check the root value against p and a value and move to subproblem on either right or left subtree
What’s the stop case?
Basic calculator III - with positive number s only, parentheses and all operators
- We anyway need to use a stack.
- Check if an variable is integer - isinstance(e, int)
- Use while loop instead of for loop
- How to truncate towards 0 when negative division
- What to do when open and closing parentheses encountered
- Reset current operation when dealing with open parentheses
Building with ocean view
Monotonic stack.
Kth largest element in an array
- initialize heap as list
- heapq.heappush
- heapq.heappop
- heap[0]
- Why do we use minimum heap here? Why do we pop when heap size is larger than k
Range sum of BST
In order traversal - try recursion
Moving average from data stream
Deque
Initialize sum as float(0)
Simplify path
- For dots, check if dots are between slashes
- When dealing with current directly or previous directly, make exception for root directory
Convert binary search tree to sorted doubly linked list
In order traversal
Sum root to leaf numbers
DFS
Simplify path
Remember to verify dots are only between slashes before moving on checking dots numbers
Remember to make exceptions for root directory slash
Insert into sorted circular linked list
- No node
- Single node
- Use two pointers and while loop
- Insert value between max and min
- Find the tail, check if insert value is beyond and min and max range
- When back to the head, just insert
Custom sort string
Frequency dictionary
3 sum closest
Sort.
Enumerate the start
Binary search and try to get closer and maintain the absolute diff.
Return target + diff
K closet points to origin
Heap
Tuple in heap
Interval list intersections
- Two pointers
- Get the start and end first
- Check if the interval is valid
- Move pointers by comparing end of the two intervals
https://leetcode.com/problems/interval-list-intersections/
Sticker to spell word
- DFS on subsequence
- For each subsequence, check which sticker to start with and exhaust the sticker by making a deep copy of it, then DFS on the remaining subsequence
- When starting with different stickers, get what’s the minimum result
- Eventually cache the minimum result for the subsequence
- Apply the DFS on entire subsequence
Word break - check if break is true or false
Start with each word and DFS and see if possible
Word break II - return all sentences that can be formed
Start with word and do DFS, if a solution is found the append to global result;
Pop from current sentence after moving to next word
https://leetcode.com/problems/word-break-ii/description/
Pow(x, n)
Binary expression
Two sum - sorted array, avoid duplicates
Low and high pointers.