6-9 2023 review deck Flashcards

1
Q

19: Remove Nth Node From End of List

A

For remove Nth node from the end of List, the 2 pointers allow us loop through the list once, fast and slow pointer, we need to set a dummy node before head, move fast Nth time, then move both fast and slow until we hit the end of the Linked list. Then slow should be at the Node before the Node that we need to remove

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

21: Merge Two Sorted Lists

A

Merge 2 sorted List - since we need to return a new list, we should create a dummy node, then build the list of the dummy node.
Compare the 2 list value, and add one at a time, the trick to remember is we might have list 1 or list 2 still have node after the while loop (while list1 and list2). So we need to account for the remainder node. We return dummy.next at the end

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

206: Reverse Linked List

A

Reverse Linked List - need to set next, prev, current pointer.
Next = current.next = prev = current = next, 4 line a circle
Return prev which the the new list beginning

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

121: Best Time to Buy and Sell Stock

A

Best time to buy and sell stock. We need to find the min price, and use today’s price - min price for the max profit, use min() to keep track of the min price and use max() to keep track of the Max profit

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

2 pointers, one fast, one slow, if slow == fast, meaning there is cycle in the linked list, the key is while fast and fast.next, if fast.next = None, then fast.next.next will error out.

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

need to return the beginning of a linked-list cycle, we can use set to add the node into the set, if the first time node appear more than once, which mean that is the beginning of the cycle. we just need to loop through the linked-list, if the linked list reach to the end and no duplicate, then return None, there is no cycle

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. Intersection of Two Linked Lists
A

Understand both set() and two pointers solution, 2 pointers is more optimal in term of using less space while set is easier to understand

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

86.Partition List

A

create 2 sublists, left list with all the lesser value, the right list with all the greater or equal value. connect the 2 lists and return at the end. one key point of the right list last node will point back to one of left list node which create a loop and error out

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

876.Middle of the Linked List

A

find the middle of the linked list, if there are 2 middle node, return the 2nd one, classic 2 pointers problem, fast and slow pointer, return slow pointer

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

23.Merge k Sorted Lists

A

extension of 21, merge 2 sorted list. Break the problem into 2 part, 1 helper function to merge 2 list, then try to reduce the lists in half each time. K lists, time logK * N

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. Remove Duplicates from Sorted Array
A

fast slow pointer, when fast pointer reach the end, return slow + 1 for the total number of unique element

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

83.Remove Duplicates from Sorted List

A

same concept like 26, expect it is for linked-list, need to remember edge case and point the slow.next = None to avoid error

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

27.Remove Element

A

modify list in place, can modify the order, return the length of the array without the target value element, is very similar to 26, instead of remove the duplicates, now remove the target value, since we don’t need to worry about the order, we can start to swap the value as soon as we see any value difference from the target value

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

283.Move zeros

A

similar to remove duplicate 26, we need to move all the non-zero element to the front and preserve the relative order, keep track on the idx, and fill the idx to len(nums) with 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  1. Reverse String
A
  • given an array with char, return the reverse version of the array - 2 pointer, l, r , swap char in place, python can do s[l], s[r] = s[r], s[l]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
  1. Two Sum II - input array is sorted
A

the array is sorted, then we can use 2 pointers left and right to find the index

17
Q
  1. Longest Substring Without Repeating Characters
A

sliding window, store the already visited value in a set(), if new char, make the right ++, if the not new char, remove the index l char, left ++, to move the sliding window

18
Q

665.Non-decreasing Array

A

need to consider the modify the left or right value, to make the whole array non-decreasing

19
Q
  1. Container With Most Water
A

area min(height_left, height_right) * right - left, move left if left <= right, other wise move right

20
Q
  1. Valid Palindrome
A

classic 2 pointers, key is move l or r when the char is not alphanumeric first

21
Q
  1. Angle Between Hands of a Clock
A

calc the hr degree and min degree, if the angle greater than 180, use 360 - result to get the smaller angle

22
Q
  1. Two Sum
A

using dict as a hashmap, the key can be target - num[i], and the value can be the index, if we get a number that match the key, then return the index of the 2 element

23
Q
  1. Valid Parentheses
A

use stack, if the char. is close, we can check the if the stack have anything and the last one element match with the open, when the loop is over, if the stack have anything, return false

24
Q
  1. Roman to Integer
A

code is simple, just a for loop to loop through the str, just need to determine if the smaller char in front of a larger one, if so, - else, just normal +

25
Q

122.Best Time to Buy and Sell Stock II

A

buy low, sell high, so compare the curr price with the prev one, if it went up, then it is a profit, add all the profit together and return

26
Q

14.Longest Common Prefix

A

use zip(*strs) to unpack and zip each char by char, if all char is the same, the set len will be 1, add that char to res

27
Q
  1. Rotate Array
A

shift the array to right by k steps, need to do it in place, so reverse the array first, then break the input array into 2 at k steps, then reverse each parts, since we use the same logic 3 times, might be good for a helper function

28
Q
  1. Group Anagrams
A

group all the str that are anagram together, use defaultdict(list), create 26 0 list, map the a to index 0 , z to index 25 in the list. add the str into res when they have the same key, Note: list can’t be a key in dict, but tuple can

29
Q
  1. Is Subsequence
A

use two pointer for s, and t, if 2 char is the same, add 1 to pointer s, at the end, compare if the pointer s same as the len(s)

30
Q
  1. Happy Number
A

it will either get to 1 eventually or it will got to a loop, use set() to check if duplicate shows up, if so , it not happy

31
Q

209.Minimum Size Subarray Sum

A

classic two pointers, first create a window that >= than the target, then shrink the left to see if we can get a smaller window size

32
Q

80.Remove Duplicates from Sorted Array II

A

2 pointers problem, the trick is we can do at most swap twice

33
Q

76.Minimum Window Substring

A

use the sliding window frame, then process the window, create a need and window dict to keep track of what we need, and what is in the window, when we find a match update valid, when valid and the len(need) is the same, we found one potential, then we try to shrink the window, we use 2 var, start and length to keep track of when we start, and min length we need for a valid solution

34
Q
  1. Find Peak Element
A

find peak element, return index, peak element mean the current element is larger than the next one, it would return any peak, binary search

35
Q

35.Search Insert Position

A

classic binary search, l = mid + 1, r = mid - 1, return l

36
Q
  1. Gas Station
A

greedy, if sum(gas) >= sum(cost), we have a solution, we only have 1 solution by the question. check each different, add to total, if total even go below 0, set res = i + 1, and check the next index

37
Q

128.Longest Consecutive Sequence

A

use a set , check to see if the left element is in the set, for example, if 100 in the set, check if 101 in the set
the start of a sequence, if a element don’t have left element, then it is a start, EX.1 , 4, there is left , 3 in the set, so 4 is not the start of of a sequence

38
Q
  1. Kth Largest Element in an Array
A

using heapq, then we have a min heap, after pop off len(nums) - k times, we get what we want, it will be better than the sorting, quickselect would also be another solution

39
Q

112.Path Sum

A

since we need to the end of leaf node, we are doing inorder DFS, use recursion. helper function would be helpful too