Max & Min arrays Flashcards
1
Q
Reverse Integer
A
- Normal reverse operation using % * and /
int newResult = result * 10 + x % 10; - Reconstruct the reverse number and check for equality
if(((newResult - x % 10) / 10) != result) return 0;
2
Q
Longest palindrome substring
A
- Expand around corner, odd and even, maxLen and llo
3
Q
No of ways of decode
A
- Classic dp with recursion and memoisation
2. Do with one char and two chars
4
Q
K Frequent Element
A
- Hashmap to store the occurence
- Min heap to store the least occurence map.get(a) - map.get(b)
- Add all the el to heap, if size > k poll
- Finally poll the last remaining element
5
Q
Max path sum
A
- Recursive
- max(0, leftSum || rightSum)
- maxPath = inOrder || maxSum
- return max(left, right) + node.val;
6
Q
Merge sorted LL
A
- l1 = mergeSort(l1.next, l2)
2. Use the same list, no need to create new LL
7
Q
Word search in a adjacent matrix
A
- Use the count var to keep track of the current idx of the word
- Handle the visited nodes, by making empty and undoing at the end
8
Q
Valid palindrome of sentences with punctuations
A
- Use two pointer
- Use character.isLetterOrDigit to consider digits as well
- Check for charAt match
9
Q
Find first and last of duplicate el in sorted array
A
- Two BST search
- First to search for the firstIndex and second is for searching the endIndex
- If target found for the first time, check whether its starting else checking on the left side
- Do the same of the ending index
10
Q
Find peak element in array
A
- BST
- If current > current + 1 search left
- If current < current + 1 search right
- end at start == end return start
11
Q
IsCelebrity
A
- for to get the possible celebrity candidate
2. If celebrity exists then the possible candidate will be compared against all other candidates
12
Q
Is tree symemtry
A
t1.val == t2.val
&& isSymmetric(t1.right, t2.left)
&& isSymmetric(t1.left, t2.right);
13
Q
Search in 2D matrix 1
A
- Consider the whole matrix as an increasing order of array
- pivotIdx = mid
- pivotValue = matrix[idx/n][idx%n]
- Rest all the binary logic
14
Q
Longest consecutive sequence
A
- Store all the el in hashset
- The logic is to find the the el - 1 not present in the set, and in the does a forloop to check whether from current no + 1 are consective and check for maxlen
15
Q
Perfect square
A
- dp, with base[0] = 0 and fill the rest with integer max;
- Iterate for i to n
- iterate for every sqr j = 1 ; j *j <= i; j++
- dp[i] = min(dp[i], dp[i - j * j] + 1)