AlgoExpert Medium Flashcards
Three Number Sum
O(N^2) Time | O(N) Space
Pattern:
- Sort the array, so that you know which side to incre/decrement
- Loop through array:
- Set left to i+1, right to last.
- If equal, append result, and incre/decrement
- If smaller increment left, if bigger decrement right
Smallest Difference
O(n log n) time | O(1) space
Pattern:
- sort the two arrays, create two idx, smallestPair, smallestDifference, currDiff
- Loop while both idx is smaller than len
- get both numbers
- If one number is larger than the other, make it the currDiff, increment the idx for smaller number
- Check if currDiff is smaller than smallest. If it is update smallest and smallest Pair
- Return smallest Pair
Move Element To End
O(N) Time | O(1) Space
Pattern:
- Set two idx, one at start, one at end of array.
- Loop while left and right:
- Loop while right == target and left < right
- If left is toMove, swap it with right (since it is not target)
- Increment left
Monotonic Array
O(N) space | O(1) time
Pattern:
- If they fluctuate between increasing and decreasing then they are neither strictly increasing or decreasing
- So set strict too boolean:
- loop from idx 1:
- Check if previous idx is < than previous
- decreasing = False
- Check if previous idx is > than previous
- increasing = False
- Check if previous idx is < than previous
- return either if true
Longest Peak
O(N) Time | O(1) space
Pattern:
- Set a variable for longestPeak and for I = 1
- Loop while I is less than second last:
- find peak by seeing if leftidx or rightidx is decreasing
- If not continue, increment i
- else, loop through left side and right side, by setting left idx 2 away and right idx 2 away
- check if they are strictly decreasing
- Get the peak length by minus right to left - 1
- Update longest
- set i to rightIdx
Array of Products
O(N) time | O(N) Space
Pattern:
- create a products array with all 1s
- create a running left product
- loop incrementally, make product[i] = running product, multiply the running product with array[i]
- create a running right product
- loop incrementally, make product[i] multiply by the running product, multiply the running product with array[i]
- return products
First Duplicate Value
O(N) time | O(N) space
Pattern:
- Create a set as seen
- Loop through the array
- If the num is in seen, return it
- add num into seen
- return -1
Spiral Traverse
O(N) Time | O(N) Space
Pattern:
- [Row] [Col]
- Set four pointers at each corner of the matrix
- Loop while opposite pointers are smaller or equal to each other
- loop through col, left to right, add result
- loop through row, up to down, add result (skips starting, to avoid double count
- Reverse loop through col, right to left, add result (skips ending)
- breaks if startrow = endrow to avoid odd counts
- Reverse loop through col, down to up, add result (skips starting)
- breaks if startCol = endCol to avoid odd counts
- incre/decrement based on pointers
Find Kth Largest Value in BST
O(N) Time | O(N) Space
Pattern:
- Since BST is left < right for every node,
- InorderTravse and append into an array
- return array[len(array)-kth]
Merge Overlapping Intervals
O(N log N) Time | O(N) Space
Pattern:
- Sort the intervals
- Have a currentInterval, since it is an array, it is mutable
- Loop through sortedIntervals:
- If currentEnd ≥ nextStart, then set currentInterval to nextEnd
- else: set currentInterval to next interval and add interval to result
Validate BST
O(N) time | O(Depth) space
Pattern:
- Use a helper function that sets minValue and maxValue to their infinities.
- Recursively traverse using helper function:
- If the node is none, return True
- if the node value is smaller than minValue return false
- if the node value is greater or equal to maxValue return false
- traverse left node with current value as new maxValue
- traverse right node with current value as new minValue
- return true if both are true
Min Height BST
O(N) Time | O(N) Space
Pattern:
- Create a helper function for recursive call
- Base condition: if right < left
- Find mid index of array
- Create BST node with mid index
- Recursively call BST node left and right with helper
- Return BST node
BST Construction: Insert()
O(N) Time | O(1) Space
Pattern:
- Set the currentNode to self
- Loop:
- If value is smaller / larger,
- If left / right is none:
- new BST node
- If left / right is not none
- set currentNode to left
- If left / right is none:
- If value is smaller / larger,
- return BST
BST Construction: Contains()
O(N) Time | O(1) Space
Pattern:
- set currentNode to the node
- Loop while currentNode is not None:
- If smaller or larger, set currentNode to left or right
- If they equal return True
- Else return false
BST Construction: Remove()
O(N) Time | O(1) Space
Pattern:
- Also use a getMin to find the smallest
- Use a parent node for same depth swapping