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
s
exactly once. Start with a pointerstart
at the beginning of the string, and amax_length
andcurrent_length
both initialized as 1. - For each character
c
ins
, ifc
has not yet occurred in our traversal ofs
, addc
to thec
hashtable with value equal to its index ins
. Incrementcurrent_length
. - If
c
exists in the hashtable, we now have a repeated element. Comparecurrent_length
withmax_length
and take the larger to be the newmax_length
. Movebegin
pasthashtable[c]
, and updatehashtable[c]
to be the current index. - Runtime:
O(n)
wheren
is 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
intervals
bystarti
using merge sort. - Using a greedy algorithm, iterate through the array to find the largest
y
such 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
,p2
withp2
atn
nodes ahead ofp1
. - When
p2
is at the end of the linked list,p1->next
will be then
-th node from the end. Setp1->next
to bep1->next->next
. - Runtime:
O(m)
wherem
is the size of the linked list.
Given an array of strings strs
, group the anagrams together.
- iterate through
strs
and sort each string. Store the sorted string in a map mapping to a vector of the strings instrs
that are anagrams. - Runtime:
O(n*k*log k)
wherek
is 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
start
coordinate, 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
target
innums
- 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)
wheren
is 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
s
character 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
false
ass
is already invalid. - If we iterate fully through
s
and the stack is non-empty, returnfalse
. Otherwise, returntrue
. - Runtime:
O(n)
wheren
is 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
next
node for the resulting linked list asLL1->val + LL2->val + carry_over
, and compute the nextcarry_over
. - WLOG, if
LL1
is empty andLL2
is non-empty, we construct thenext
node asLL2->val + carry_over
and compute the nextcarry_over
. - Runtime:
O(max{LL1.size(), LL2.size()})