0% found this document useful (0 votes)
5 views2 pages

Report

The document outlines various algorithms and techniques for solving common programming problems, including sorting methods like Insertion Sort and Merge Sort, as well as searching algorithms such as binary search. It also discusses specific algorithms like Kadane’s for maximum subarray sums, the Dutch National Flag algorithm for sorting colors, and methods for handling string and array manipulations. Key strategies include using hashmaps for Two Sum problems, stack operations for parentheses validation, and efficient approaches for binary addition.

Uploaded by

letiho5049
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Report

The document outlines various algorithms and techniques for solving common programming problems, including sorting methods like Insertion Sort and Merge Sort, as well as searching algorithms such as binary search. It also discusses specific algorithms like Kadane’s for maximum subarray sums, the Dutch National Flag algorithm for sorting colors, and methods for handling string and array manipulations. Key strategies include using hashmaps for Two Sum problems, stack operations for parentheses validation, and efficient approaches for binary addition.

Uploaded by

letiho5049
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Arrays

Dutch National Flag algo – separate algo into 0s, 1s and 2s


Boyer-Moore Majority Voting Algorithm – find majority element
Kadane’s algo – contiguous max sum subarray

Insertion Sort
Pair-wise swaps to insert new element to maintain sorted order
Two pointers – one for new element and one for where to swap it
O(n) for each swap and compare (one step)
O(n2) for reverse sorted array
Compares can be sometimes more expensive than swaps – use binary search in A[0 – i-1]
which is already sorted. O(log i)
Overall O(n log n) for compares instead of O(n2)
Requires insertion at right positions which may require shifting things to the right

Merge Sort
Recursive algo using merge
Divide and conquer split arrays
O(n log n)
T(n) = c1 + 2T(n/2) + cn
N leaf nodes and log n +1 levels

When the question asks for maximum/minimum and the constraints are large (ie., ~10^5),
there is a probability of binary search.

Kadane’s algorithm
basic idea is if adding the current element to the sum makes the sum negative, we need to
discard it and start a new subarray from the next element.

Parantheses
Faced problems with single length input as the stack would be empty and there is nothing to
compare or pop from the stack. Added simple stack empty checks to work around it.

Two Sum
Using hashmap we can keep target-current element and current element. If any future
element exists as the key in the map, we can easily return the value of the key and the
current element.
Similar approach can be used for Three sum.

Merge Sorted Lists


Using merge strategy learnt in Merge Sort lecture, we can compare element pairs and
combine the arrays into result array. We also need to iterate over each array after the merge
in case one of them contains more elements than the other.

Length of Last Word


Using split function in Java is the easiest way to do this question. Split by whitespace and
return length of last element in the returned split array.
Faster way is to reverse iterate the array, skip over whitespace, and start counting once we
encounter a letter and when return the length when we see a whitespace.

Binary Addition
Ignoring the constraints, we can easily convert each of the inputs into integers, then add
them and then convert them back to binary string. But, due to the length of the inputs, the
integers will not fit in 64-bit space. We actually need to perform binary addition, bit-by-bit,
tracking carry and for each addition.
Learnt binary addition for solving this question.

Search Insert Position


Since the array of sorted, binary search is the fastest way to find the target value. If the value
is not in the input array, we can return left pointer as the insert position.

Remove Element
Ignoring the in-place constraint, we can make a separate array.
Otherwise, we need to maintain a pointer for insert position and skip over the target values.
Since, order of the result array does not matter, swapping, even though it changes order of
elements, it does not create an issue.

Find the Index of the First Occurrence in a String


Since we are searching for a substring, it makes sense to use a sliding window. We use
nested loop and keep incrementing the iterator if the needle character matches the haystack
character. If the window length is equal to needle length, we have found it. Otherwise, move
to next window.

You might also like