General Flashcards
An array has every distinct number three times except one. Find this number
Count how many times each bit is on. This will be of the form 3n or 3n+1. The bit which is of the form 3*n+1 is on in answer.
Reverse Linked List
last = NULL;
while head is not NULL:
next = head->next
head->next=last
last=head
head=next
return last;
Find maximum product subarray with negative, positive and zeroes.
https://leetcode.com/problems/maximum-product-subarray/discuss/48230/Possibly-simplest-solution-with-O(n)-time-complexity
builtin_clz(x)
builtin_ctz(x)
builtin_popcount(x)
count leading zeroes
count trailing zeroes
count set bits
Given a string A and integer B, what is maximal lexicographical string that can be made from A if you do atmost B swaps.
Keep a global max string.
Recursive function is f(string,k). We run a n^2 loop and try all swaps and call recursive function f(string,k-1) and check if string is better than max string.
Each recursive call has a time complexity of O(n^2+n)
Total k recursive calls
Time Complexity O((n^2)^k)
Improvement is rather than trying all swaps we maintain a current index as well and try swaps with the max number only after it.
Each recursive function calls itself n times until k is 0
O(n^k). if k doesn’t change, current index changes with only one recursive call therefore no effect on time
Pure greedy wrong at 5499
https://www.geeksforgeeks.org/find-maximum-number-possible-by-doing-at-most-k-swaps/
Number of partitions of N
dp(n,taken) number of ways to partition n such that least number is taken
dp(n,taken) = dp(n-taken,taken)+dp(n,taken+1)
either taken is present or it isn’t
O(n^2)
Knapsack
Given N items with (value, weight), total weight W of knapsack, find maximum value you can get
dp(i,w) = maximum value we can create with choices being from the first i elements.
dp(i,w) = max( v[i] + dp(i-1, w-w[i] ), dp[i-1][w] )
O(NW)
Knapsack
Given N items with (value, weight), total weight W of knapsack, find maximum value you can get
infinite items available
dp(i,w) = max( dp(i-1,w), v[i] + dp(i-1,w-w[i]), 2v[i] + dp(i-1,w-2w[i]) …
O(nww)
check what dp(i,w-w[i]) is
we can use that value to reduce time complexity
dp(i,w) = max(dp(i-1,w), v[i] + dp(i,w-w[i]) )
Longest Increasing Subsequence
dp[i] = LIS ending at index i
dp[i] = max( 1 + dp[j] ) for all j < i such that a[j] < a[i]
O(n^2)
dp[i] = among all increasing subsequences of length i, dp[i] will be the minimum ending number.
for some number x, it can be added to any subsequence with dp[j] < x. j = lowerbound(x) on dp,
dp[j]=min(dp[j],x)
O(nlogn)
Longest common subsequence of two arrays. Distinct integers in array.
LCS -> O(n^2)
a = [4, 3, 5, 7, 9]
b = [4, 3, 2, 9, 3]
map all elements of a to their index.
ai = [1,2,3,4,5]
bi = [1,2,6,5,2]
LIS is answer
O(nlogn)
Longest common subsequence
dp[i][j] = LCS of s1[0,i] and s2[0,j]
dp[i][j] = max( dp[i-1][j], dp[i][j-1] )
if s1[i] == s2[j]
dp[i][j] = 1 + dp[i-1][j-1]
O(n*m)
Players p1 and p2. Given array p1 takes one element from front or back. Then p2. Maximize sum of p1-sum of p2.
dp[l][r] = maximum s1-s2
dp[l][r] = max( a[l] - dp[l+1][r], a[r]-dp[l][r-1] )
O(n^2)
Given an array. Operation: merge i and i+1 to create (a[i]+a[i+1])%100 which gives profit a[i]*a[i+1]. Find maximum profit after all operations.
dp{l}[r] = max profit for array [l,r]
dp[l][r] = max (dp[l][mid]+dp[mid+1][r] + (pre[r]-pre[mid])%100*(pre[mid]-pre[l-1])%100) over all mid from l to r-1
O(n^3)
Given array of N elements, for each window of K elements, find the maximum element.
Expected O(n)
Useful element is element which is greater than all elements on it’s left for that window.
We maintain a deque of useful elements which will store the elements in reverse sorted order.
When iterating, keep removing the last element until it is greater than x. So the max element will always be at the front.
Given an array. Operation: merge i and i+1 to create ( a[i]X + a[i+1]Y + Z )%50 which gives profit a[i]*a[i+1]. Find maximum profit after all operations. n<=50
dp[l][r][k] = maximum profit from range(l,r) such that value becomes k.
dp[l][r][(pX + qY + Z)%50] = max ( dp[l][mid][p] + dp[mid+1][r][q] + p*q) over all p,q,mid
O(n^5)
Given an array we can delete consecutive same elements to get a profit of (number of elements)^2. Find max profit we can get by deleting the entire array.
See LR dp states ending problem
Given a parenthesis sequence with ‘?’, find the number of ways to replace ‘?’ with ‘(‘ or ‘)’ such that final string is balanced.
dp[i][j] = number of ways such that the prefix (1..i) has value j. Ans is dp[n][0].
Given a string, find the number of distinct subsequences.
dp[i] = number of distinct subsequences of string
j = last occurence of s[i]
(1..i). dp[i]=2*dp[i-1]-dp[j-1]
No of ways to fill N blanks with 0 or 1, such that no K consecutive characters are same.
dp[i][j][k] = number of ways to fill first i blanks such that last character is j, with k consecutive characters at the end.
n2k
Keys: A, ctrl-A, ctrl-C, ctrl-V
Maximum A’s we can create with N key presses.
dp[n] = max of dp[n-1]+1, for i >= 3 dp[n-i]*(i-1)
O(n^2)