LeetCode Revision Flashcards

1
Q

Minstack

A

Insert 2x - current_min, Pop, check if less than min, then return min and update min to next min. Top, return if not min, else return min.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. Symmetric Tree
A

Create recursive function. Check if left and right exist and are equal. Then check if the mirrors are equal. Mirrors are left.left and right.right, left.right and right.left

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

198 House robber

A

Create a DP cache. Keep updating values. First two are, A[0] and max(A[0], A[1]). The repetition formula is if A[n-2] + A[n] > A[n-1] then A[n] = A[n-1] + A[n]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. Palindrome Linked List
A

Reverse first half of the list. Take 3 pointers, fast, slow, and rev. Increment fast by 2. set rev, rev.next, slow = slow, rev, slow.next, this reverses the linked list.
After reversing if fast is still not null, then there are odd number of elements. If so, increment fast by 1.
Starting from rev, slow which are now both heads of respective lists, keep incrementing and checking for sameness.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. Valid Palindrome
A

use regular expression to strip the spaces and punctuations and then reverse and compare. This is regular expression: ‘[\W_]+’ and to reverse string you should use str[::-1]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. Reverse Bits
A

Take 0. Left shift by 1 and or with last bit of n. Then right shift n by one. Repeat for 32 times.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

108 Convert sorted array to BST

A

Use recursion to insert middle element.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly