9 Chapter Algorithms¶
Lecture 1 从strStr谈面试技巧与Coding Style¶
# | Problem | Comment | Date |
---|---|---|---|
1 | Implement strStr() | 08/07/2017, | |
2 | Subsets | No duplicates | 08/07/2017, |
3 | Subsets II | With duplicates | 08/07/2017, |
4 | Permutations | No duplicates | 08/07/2017, |
5 | Permutations II | with duplicates | 08/07/2017, |
6 | Combinations | from {1, ... n}, choose k of them | 08/07/2017, |
7 | Combination Sum | No duplicate, repeat selection allowed | 08/09/2017, |
8 | Combination Sum II | With duplicates, each can be select once | 08/09/2017, |
9 | Combination Sum III | {1, ..., 9} select k numbers and add to n, (See K Sum) | 08/09/2017, |
10 | Combination Sum IV | (Same as Backpack VI) | 08/09/2017, |
11 | Letter Combinations of a Phone Number | 08/07/2017, | |
12 | Palindrome Partitioning | 08/07/2017, | |
13 | Palindrome Partitioning II | C++版本如何通过大的Input | 08/07/2017, |
14 | Restore IP Address | 08/07/2017, |
Notes¶
- strStr() problem
- 不用那么的复杂的算法,但要做到bug free(检查corner case, base case, 程序缩进)。
- 面试官
- 代码工整 (形成自己的style)
- coding习惯(corner test case,extreme case考虑到了没)
- 沟通能力,交流障碍有吗
- 面试考察基本功
- 程序风格(缩进,括号,变量名)
- Coding习惯(异常检查,边界处理)
- 沟通(让面试官时刻明白你的意图)
- 测试(主动写出合理的Testcase)
- 如何准备面试
- 如何一个月搞定算法面试?总结归类相似题目
- 找出同类题型的模板程序
- 拿offer四大法宝
- 摆正心态,尽可能多地刷题,不急于求成,做完一类再来下一类
- 愉快交流,
- 理解而非背诵,不记具体实现记这一类题通用思维方式和分析技巧。
- 时间要用在刀刃上,不要把时间浪费在那些基本不会考你又很心虚的内容(比如KMP,红黑树,AVL,ACM竞赛题)
Lecture 2 二分搜索与旋转排序数组 Binary Search & Rotated Sorted Array¶
# | Problem | Comment | Date |
---|---|---|---|
1 | Classical Binary Search* | 08/12/2017, | |
2 | Search for a Range | 08/12/2017, | |
3 | Search Insert Position | 08/12/2017, | |
4 | Search in a Big Sorted Array | 08/12/2017, | |
5 | Search a 2D Matrix | 08/12/2017, | |
6 | Search a 2D Matrix II | 08/15/2017, | |
7 | First Bad Version | 08/15/2017, | |
8 | Find Peak Element | Google, important, mit algorithm course. | 08/15/2017, |
9 | Recover Rotated Sorted Array* | 08/15/2017, | |
10 | Find Minimum in Rotated Sorted Array | 08/15/2017, | |
11 | Find Minimum in Rotated Sorted Array II | 08/15/2017, | |
12 | Search in Rotated Sorted Array | 08/15/2017, | |
13 | Search in Rotated Sorted Array II | 08/15/2017, | |
14 | Median of Two Sorted Arrays | 08/15/2017, | |
15 | Rotate String* | 08/15/2017, | |
16 | Reverse Words in a String | 08/15/2017, | |
17 | Wood Cut* | 08/15/2017, |
Notes¶
- Classical Binary Search
- Given an sorted integer array - nums, and an integer - target. Find the any/first/last position of target in nums, return -1 if target doesn’t exist.
- Prototype:
int binarySearch(int[] nums, int target)
- 复杂度分析
- T(n) = T(n/2) + O(1) = O(logn)
- 算法面试中如果需要优化O(n)的时间复杂度那么只能是O(logn)的二分法
- Recursion or While-loop?
- bad for recursion: Linux have only 8MB stack, local variable takes much space for each level of recursion.
- for simple problem, use iteration instead of recursion. for more complex problem use recursion. ask interviewer.
- Binary Search模板四要素
- start + 1 < end
- start + (end - start) / 2
- A[mid] == , < ,>
- A[start] A[end] ? target
- Find First Position of XXX VS. Find Last Position of XXX
- character of binary search
- the split depends on you need the first position of XXX or last position of XXX. See the code.
Lecture 3 二叉树问题与分治算法 Binary Tree & Divide Conquer Algorithm¶
# | Problem | Comment | Date |
---|---|---|---|
1 | Binary Tree Preorder Traversal | stack, right push first | 08/26/2017, |
2 | Maximum Depth of Binary Tree | Recursion, same as find height of the tree | 08/26/2017, |
3 | Balanced Binary Tree | find height first | 08/26/2017, |
4 | Lowest Common Ancestor of a Binary Tree | Lowest Common Ancestor of a Binary Search Tree | 08/26/2017, |
5 | Binary Tree Maximum Path Sum | "at least one node" | 08/26/2017, |
6 | Binary Tree Level Order Traversal | BFS, DFS, queue | 08/26/2017, |
7 | Binary Tree Level Order Traversal II | call reverse() when done by Binary Tree Level Order Traversal | 08/26/2017, |
8 | Binary Tree Zigzag Level Order Traversal | use two stacks | 08/26/2017, |
9 | Validate Binary Search Tree | inorder traversal, LONG_MAX, LONG_MIN | 08/26/2017, |
10 | Search Range in a Binary Search Tree* | inorder traversal | 08/26/2017, |
11 | Insert a Node in Binary Search Tree* | recursion | 08/26/2017, |
12 | Binary Search Tree Iterator | use stack | 08/26/2017, |
13 | Remove Node in Binary Search Tree* | Problem Inorder Successor BST | 08/26/2017, |
14 | Inorder Successor BST | Successor of a node, special case: [2,1],p=1, [3,1,null,null,2], p=2. | 08/26/2017, |
Notes¶
- 构造binary tree 唯一或者不唯一
- preorder + inorder
- postorder + inorder
- preorder + postorder 不唯一
- divide conquer solve preorder traversal O(n^2)
- Max Path Sum
- root -> leaf
- root -> any node.
- any node -> any node 存在负数节点,这条路径至少包含一个结点
Lecture 4 动态规划 Dynamic Programming I¶
# | Problem | Comment | Date |
---|---|---|---|
1 | Triangle | 08/29/2017, | |
2 | Minimum Path Sum | 08/16/2017, | |
3 | Unique Paths | 08/16/2017, | |
4 | Unique Paths II | 08/16/2017, | |
5 | Climbing Stairs | 08/30/2017, | |
6 | Jump Game | 08/16/2017, | |
7 | Jump Game II | Maximum Subarray, Frog Jump | 08/30/2017, |
Notes¶
- 从递归到动归 - Triangle
- DFS - Traverse
- 复杂度
O(2^n)
n is the height (每次有两个可选路径) - DFS - Divide & Conquer
- 复杂度
O(2^n)
每条路劲没有任何条件让它提前返回 - 通过画图来观察如何降低复杂度(有重复计算)
- 复杂度
- Divide & Conquer + Memorization
- 记忆化搜索,装了眼睛,知道停哪
- Bottom-up 动归的四个步骤
- 状态的定义
- 初始化,终点先有值
- 循环递推(不是递归)求解
- 求结果: 起点
- Top-down
- 两种没有太多的优略之分,根据个人习惯灵活运用
- 什么样的题适合使用动态规划?
- 满足三个条件之一则极有可能使用动归求解
- Maximum/Minimum
- Yes/No
- Count(*) (i.e. 总共有多少个)
- 满足三个条件之一则极有可能使用动归求解
- 不能用动归
- 要求出所有“具体”的方案而非方案“个数” http://www.lintcode.com/problem/palindromepartitioning/
- 输入数据是一个“集合”而不是“序列” http://www.lintcode.com/problem/longest-consecutivesequence/
- 动态规划4点要素
- 状态 State
- 灵感,创造力,存储小规模问题的结果
- 方程 Function
- 状态之间的联系,怎么通过小的状态,来算大的状态
- 初始化 Intialization
- 最极限的小状态是什么, 起点
- 答案 Answer
- 最大的那个状态是什么,终点
- 状态 State
- 面试最常见的四种类型
- Matrix DP (15%)
- state: f[x][y] 表示我从起点走到坐标x,y……
- function: 研究走到x,y这个点之前的一步
- intialize: 起点
- answer: 终点
- Sequence DP (40%)
- state: f[i]表示“前i”个位置/数字/字母,(以第i个为)...
- function: f[i] = f[j] … j 是i之前的一个位置
- intialize: f[0]..
- answer: f[n-1]..
- Two Sequences DP (40%)
- Others (5%)
- Matrix DP (15%)
- 面试中常用动态规划
- 矩阵动态规划(坐标相关)
- 单序列动态规划(上)
Lecture 5 动态规划 Dynamic Programming II¶
# | Problem | Comment | Date |
---|---|---|---|
1 | Palindrome Partitioning II | 08/16/2017, | |
2 | Word Break | 08/16/2017, | |
3 | Longest Common Subsequence* | 08/16/2017, | |
4 | Longest Common Substring* | 08/16/2017, | |
5 | Edit Distance | 08/16/2017, | |
6 | Distinct Subsequences | 08/16/2017, | |
7 | Interleaving String | 08/16/2017, |
Lecture 6 链表 Linked List¶
# | Problem | Comment | Date |
---|---|---|---|
1 | Remove Duplicates from Sorted List II | 08/30/2017, | |
2 | Reverse Linked List II | 08/30/2017, | |
3 | Merge Two Sorted Lists | 08/30/2017, | |
4 | Partition List | 08/30/2017, | |
5 | Sort List | 08/30/2017, | |
6 | Reorder List | 08/30/2017, | |
7 | Merge k Sorted Lists | 08/30/2017, | |
8 | Copy List with Random Pointer | 08/30/2017, | |
9 | Linked List Cycle | 08/30/2017, | |
10 | Convert Sorted List to Balanced Binary Search Tree | 08/30/2017, | |
11 | Convert Binary Tree to Doubly Linked List | 08/30/2017, | |
12 | Reverse List Nodes in k-Groups | 08/30/2017, |
Notes¶
- Basic skills
- Insert a Node in Sorted List
- Remove a Node from Linked List
- Reverse a Linked List
- Merge Two Linked Lists
- Find the Middle of a Linked List
- Sorting
- quick sort, 原地排序,不稳定。相对顺序会改变,所以不稳定。
- 稳定排序,
- 睡眠排序
- Fast Slow Pointers
- Find the Middle of Linked List
- Remove Nth Node From End of List
- Linked List Cycle I, II
Lecture 7 数组与数 Array & Numbers¶
# | Problem | Comment | Date |
---|---|---|---|
1 | Recover Rotated Sorted Array* | 08/30/2017, | |
2 | Rotate String* | 08/30/2017, | |
3 | Reverse Words in a String | 08/30/2017, | |
4 | Merge Sorted Array | 08/30/2017, | |
5 | Merge Sorted Array II* | 08/30/2017, | |
6 | Median of Two Sorted Arrays | 08/30/2017, | |
7 | Maximum Subarray | 08/30/2017, | |
8 | Maximum Subarray II* | Prefix sum, left, right | 08/30/2017, |
9 | Maximum Subarray III* | DP, from O(n 2k)O(n2k) to O(nk)O(nk) | 08/30/2017, |
10 | Minimum Subarray* | 08/30/2017, | |
11 | Subarray Sum* | Subarray Sum Equals K | 08/30/2017, |
12 | Best Time to Buy and Sell Stock | 08/30/2017, | |
13 | Best Time to Buy and Sell Stock II | 08/30/2017, | |
14 | Best Time to Buy and Sell Stock III | 08/30/2017, | |
15 | Two Sum | 08/30/2017, | |
16 | 3Sum | 08/30/2017, | |
17 | 4Sum | 08/30/2017, | |
18 | K Sum* | 08/30/2017, | |
19 | Partition Array | 08/30/2017, | |
20 | Sort Colors | 08/30/2017, | |
21 | Sort Letters by Case | 08/30/2017, | |
22 | Interleaving Positive and Negative Numbers | 08/30/2017, |
Notes¶
- 三步翻转法
- Recover Rotated Sorted Array
- Median of Two Sorted Arrays (Important)
- find median ==> find kth largest
- take O(1) to reduce the problem to find the k/2 th element.
- Maximum Subarray == Best Time to Buy and Sell Stock (see the video at 53:00)
- 前缀和
Lecture 8 数据结构 Data Structure¶
# | Comment | Date |
---|---|---|
题目 | 备注 | 日期 |
Min-Stack | ||
Implement a queue by two stacks | ||
Largest Rectangle in histogram | 递增栈 | |
Max Tree | 递减栈 | |
Rehashing | ||
LRU Cache | ||
Longest Consecutive Sequence | ||
Subarray Sum | ||
Copy List with Random Pointer | ||
Anagrams | ||
Median Number | ||
Heapify | ||
Merge K sorted List | ||
Word Search II | 08/30/2017, |
Notes¶
- Running time complexity for stack operation or other data structure
- talk average instead of worst case.
- think in the larger
- For the largest Rectangle in histogram problem, considering the concept of 递增栈
- 强化班内容:递增栈其他应用,表达式值,表达式树
- Largest Rectangle in Histogram: 思路模板:“这种题目的相似性是找数组里面一个元素左边或者右边第一个比他小或者比他大的元素"
- 并查集,两个指针,hashheap,动态规划优化题目
- Hash implementation:
int hashfunc(string key){ int sum = 0; for (int i = 0; i < key.length(); i++) { sum = sum * 33 + (int)(key.charAt(i)); sum = sum % HASH_TABLE_SIZE; } return sum }
- Collision
- open hashing
- closed hashing
Lecture 9 图论与搜索 Graph & Search¶
题目 | 备注 | 日期 |
---|---|---|
Clone graph | ||
Copy List with Random Pointer | ||
Find the connected |
Notes¶
- time for prepare interview
- easy: 5 min
- medium: 15 min
- hard: 30-40 min
- 最好不用DFS, node数量多的情况下,recursion spaec消耗太大,
- Topological sorting
- inDegree
- outDegree
- 何适用BFS,
- 从一个点出发,找连通块
- level by level, 寻找两node最短距离
- subset
- 隐式图搜索 VS 显式图搜索
- 何时用DFS,
- 找出所有的结果而不是找出总结果数。
- Running time permutation: O(n! * n)
- Running time subset: O(2^n * n)
- 搜索类型:
- 显式图搜索
- Matrix
- Graph
- 隐式图搜索
- permutation
- subset
- combination
- 对分割类,可以把问题看成分割线的combination problem.
- 显式图搜索