Tracking my progress on the LeetCode Top Interview Questions
- Rotate Image
- Longest Substring Without Repeating Characters
- Merge Intervals
- Remove N-th Node From End of List
- Group Anagrams
- Insert Interval
- Pow(x,n)
- Find First and Last Position of Element in Sorted Array
- Validate Binary Search Tree
- Valid Parenthesis
- Add Two Numbers
Given an n x n matrix representing an image, rotate the image by 90 degrees (clockwise).
- Perform a matrix transpose on
matrix(i.e. flipping it along its diagonal) - Reverse every row in the transposed matrix.
- Runtime:
O(n^2)(or linear in the number of cells inmatrix).
Given a string s, find the length of the longest substring without repeating characters.
- Utilize a sliding window algorithm and hashtable.
- Iterate through
sexactly once. Start with a pointerstartat the beginning of the string, and amax_lengthandcurrent_lengthboth initialized as 1. - For each character
cins, ifchas not yet occurred in our traversal ofs, addcto thechashtable with value equal to its index ins. Incrementcurrent_length. - If
cexists in the hashtable, we now have a repeated element. Comparecurrent_lengthwithmax_lengthand take the larger to be the newmax_length. Movebeginpasthashtable[c], and updatehashtable[c]to be the current index. - Runtime:
O(n)wherenis the length of the strings.
Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
- Preprocessing: sort
intervalsbystartiusing merge sort. - Using a greedy algorithm, iterate through the array to find the largest
ysuch thatintervals[i]tointervals[y]are all in the same overlapping interval. - Runtime:
O(n log n)
Given the head of a linked list, remove the n-th node from the end of the list and return its head.
- Keep two pointers,
p1,p2withp2atnnodes ahead ofp1. - When
p2is at the end of the linked list,p1->nextwill be then-th node from the end. Setp1->nextto bep1->next->next. - Runtime:
O(m)wheremis the size of the linked list.
Given an array of strings strs, group the anagrams together.
- iterate through
strsand sort each string. Store the sorted string in a map mapping to a vector of the strings instrsthat are anagrams. - Runtime:
O(n*k*log k)wherekis the length of the longest string.
Given an array of non-overlapping intervals in ascending order (based on the start of the intervals), insert a new interval such that the array is still in ascending order and there are no overlapping intervals.
- Since the array is sorted by
startcoordinate, we run a binary search to see where the new interval should be inserted. - Insert the new interval, and then run
merge(from solution for Merge Intervals) - Runtime:
O(n log n)
Implement x^n.
- Use the fast modular exponentiation algorithm
- Runtime:
O(log n)
Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.
- Use a modified binary search to find the leftmost and rightmost occurences of
targetinnums - Runtime:
O(log n).
Given the root of a binary tree, determine if it is a valid binary search tree.
- Store node values in a list via an inorder, depth-first traversal of the binary tree.
- Determime whether the resulting list is sorted in strictly increasing order. If so, it is a valid BST.
- Runtime:
O(n)wherenis the number of nodes in the tree rooted atroot.
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid; that is, open brackets are closed by the same type of bracket, and open brackets are closed in the correct order.
- Iterate through
scharacter by character. We will be utilizing a stack data structure. - If we encounter an open bracket, push it onto the stack.
- If we encounter a closed bracket and the top of the stack is its corresponding opening bracket, then pop the stack. Otherwise, return
falseassis already invalid. - If we iterate fully through
sand the stack is non-empty, returnfalse. Otherwise, returntrue. - Runtime:
O(n)wherenis the length ofs.
Given two non-empty linked lists LL1 and LL2 representing two non-negative integers - where the digits are stored in reverse order, and each node contains a single digit - add the two numbers and return the sum as a linked list. We can assume that there are no leading zeros except for the number 0 itself.
- In regular addition we start adding from the least significant digit and the carry-over moves on to the next digit. Since the linked list stores digits from most to least significant, we flip this logic.
- While both linked lists are non-empty, we construct the
nextnode for the resulting linked list asLL1->val + LL2->val + carry_over, and compute the nextcarry_over. - WLOG, if
LL1is empty andLL2is non-empty, we construct thenextnode asLL2->val + carry_overand compute the nextcarry_over. - Runtime:
O(max{LL1.size(), LL2.size()})