Leet Code Summary and Reflection

 

Array

Tips: keep two points and shrink from left and right to center, check to meet certain requirement (sum equals a target number) until left index is over right one.

Problems: Two Sum, 3Sum, 3Sum Closest, 4Sum, etc.

 

Linked List

Tips:

  1. combine with other data structure like tree, min heap
  2. combine with other algorithms like merge and quick sort
  3. most time use two pointers (slow and fast) to check circular or not, sometimes to find the middle point in linked list
  4. most interview questions emphasize changing node position and keep linking
  5. Take into account whether the change is taking place at the head of the list or someplace else.

Problems: Add Two Numbers, Convert Sorted List to Binary Search Tree, Merge Two Sorted Lists, Partition List, Remove Duplicates from Sorted List, Remove Duplicates from Sorted List II, Remove Nth Node From End of List, Reverse Linked List II, Reverse Nodes in k-Group, Rotate List, Swap Nodes in Pairs

 

Stack

Tips: Input is reversal order of output (Stacks can provide a reversal characteristic.)

Problems: binary tree inorder/preorder/postorder traversal (stack can hold temp node and queue has to deal with it immediately)

 

Dynamic Programming (DP)

Tips:

  1. divide the problem into smaller pieces (smallest as base case)
  2. solve the smaller pieces in some way (toward base case)
  3. reassemble the whole problem to get the result
  4. most times it can be abstracted as: subproblem is moving from a point with 2 directions (right, down), which starts from a certain point (base case) whose condition is fixed, either it’s the destination (bottom right node in a matrix) or elements in last row or level of a tree.

Problems: unique path, triangle, maximal rectangle, minimum path sum, etc.

 

Binary Search

Tips:

Binary tree preorder traversal: easiest one, it could process the current, current left and right nodes immediately, so it doesn’t have to put current node in while loop condition as in-order and take care of previous node.

Binary tree inorder traversal: it has to take care of the current node (record left, self and right order as well, can replace with set) or it can use stack peek method and set (current node is visited or not) to check and then deal with left, self, right node in order.

Binary tree postorder traversal: it has to use a stack and two pointers (current, previous) to check left node, left and right have been visited or not, if not push left and right one by one in stack, then process the current node, if yes, process the current node directly.

Clone Graph: BFS traversal all nodes in neighborhood and store in a queue, then process these nodes one by one. Set to store visited nodes to avoid unlimited cycling, HashMap to store all existing nodes to avoid duplicates and Queue to store nodes in neighborhoods.

Best Time to Buy and Sell Stock: get the min number and the larger one after it (only trade once), or if the current one is larger than previous then trade (trade unlimited times), get max profit so far and max profit from current to the end (trade twice)

Search in Rotated Sorted Array: check which half is sorted, and then narrow the range based on the target number in certain half, skip duplicate input numbers if necessary.

Subsets: recursive, when reach the required number limitation and then return the current subset, skip duplicate input numbers if necessary.

 

 

Recursion (recursion as a form of iteration)

Tips:

  1. Recursion is a method of solving problems that involves breaking a problem down into smaller and smaller subproblems until you get to a small enough problem that it can be solved trivially.
  2.  Usually recursion involves a function calling itself.

Problems: Combination I, Combinations II, Combination Sum, Letter Combinations of Phone Numbers: recursively process the number sequence, add to the temporary results collection, go to next level until reach certain condition (depth equals to length of number array), return upper level and remove the last element added into the temporary results collection, continue to process next number.

 

Big O

Hardware is different, so need a way to measure program efficiency and then use how many times instructions run and how much space (byte, bit) take to finish the program

Donate $5 to me for a coffee with PayPal and read more professional and interesting technical blog articles. Feel free to visit my web app, WhizWallet, to apply for credit, store or gift cards, DealsPlus to browse daily deals and store coupons to save money.
Follow me @Yaoli0615 at Twitter to get latest tech updates.
Resources:
Advertisements

About liyao13

Yao Li is a web and iOS developer, blogger and he has a passion for technology and business. In his blogs, he shares code snippets, tutorials, resources and notes to help people develop their skills. Donate $5 to him for a coffee with PayPal at About Me page and read more professional and interesting technical blog articles. Follow him @Yaoli0615 at Twitter to get latest tech updates.
This entry was posted in CS Research&Application, Intern&Job, IT, Uncategorized and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s