1. 回顾数据结构&算法
让我们重新回顾一下数据结构的整体分类和算法,并自己内心说明各个数据结构的特性,回忆算法的思想和代码模板。
1.1 一维数据结构
基础的有:数组(查询快,插入操作较慢,需要重新分配空间)、链表(查询慢,插入比数组快)
高级的有:栈stack(先进后出)、队列queue(先进先出)、双端队列deque(适用于FIFO和LIFO两个场景)、集合Set(无重复元素集合)、映射map(hash 或者 map)、etc
1.2 二维数据结构
基础的有:树tree、图Graph(链表是特殊化的树,树是特殊化的图)
高级的有:二叉搜索树binary search tree (red-black tree,AVL)、堆heap、并查集disjoint Set、字典树 Trie、etc
1.3 特殊
位运算 bitwise、布隆过滤器 BloomFilter
LRU Cache
注意:在脑海中回忆数据结构的特点
1.4 算法
- if-else,switch ——> branch
- for,while loop ——> Iteration
- 递归 Recursion(Divide & Conquer,backtrace)
- 搜索Search:深度优先搜索 Depth first Search, 广度优先搜索Breadth first search , A*,etc
- 动态规划 Dynamic Programming
- 二分查找 binary Search
- 贪心算法 Greedy
- 数学 Math,几何 Geometry
注意:在脑海中回忆上面的各种算法思想和代码模板
在脑海中回忆这四种算法的思想和代码模板:分治 + 回溯 + 递归 + 动态规划
递归代码模板:**
public void recur (int level, int param) {
// terminator
if (level > MAX_LEVEL) {
# process result
return ;
}
// process current logic
process(level,param);
// dirll down
recur(level + 1, newParam);
// restore current result
}
分治 Divide & Conquer代码模板:
def divide_conquer(problem,param1,param2,...):
# recursion terminator
if problem is None:
print_result
return
# process data
data = process_data(problem)
subproblems = split_problem(problem, data)
# conquer subproblems
subresult1 = self.divide_conquer(subproblems[0], p1,...)
subresult2 = self.divide_conquer(subproblems[1], p1,...)
subresult3 = self.divide_conquer(subproblems[2], p1,...)
...
# process and generate the final result
result = process_result(subresult1,subresult2,subresult3,...)
# revert the current level status
注意-重点:
- 拒绝人肉递归,费时耗力不讨好
- 找到最近最简方法,将其拆分为可重复解决的entity
- 数学归纳法思维,简单高效反天性
2. 动态规划
2.1 动态规划定义
1.Wiki 定义:
https://en.wikipedia.org/wiki/Dynamic_programming
2.”Simplifying a complicated problem by breaking it down into simpler sub-problems” (in a recursive manner)
3.Divide & Conquer + Optimal substructure
分治 + 最优子结构
关键点:
- 动态规划和递归、分治没有根本上的区别(关键还是要看有没有最优子结构)
- 共性:找到重复子问题
- 差异性:最优子结构、中途可以淘汰次优解
2.2 实例案例
- 斐波拉契数列
- 路径计数
- 最长公共子序列
2.2.1 斐波拉契数列
2.2.2 路径计数
动态规划的关键点:
- 最优子结构 opt[n] = best_of(opt[n-1], opt[n-2], …)
- 储存中间状态:opt[i]
- 递推公式(美其名曰:状态转移方程或者 DP 方程)
Fib: opt[i] = opt[n-1] + opt[n-2]
二维路径:opt[i,j] = opt[i+1][j] + opt[i][j+1] (且判断a[i,j]是否空地)
2.2.3 最长公共子序列
https://leetcode-cn.com/problems/longest-common-subsequence/
2.2.4 动态规划小结
- 打破思维惯性,形成机器思维
- 理解复杂逻辑的关键
- 这也是职业进阶的要点
扩展:MIT algorithm course(B站的麻省理工算法课程)
https://www.bilibili.com/video/av53233912?from=search&seid=2847395688604491997
3. 练习
- https://leetcode-cn.com/problems/climbing-stairs/description/
- https://leetcode-cn.com/problems/triangle/description/
https://leetcode-cn.com/problems/maximum-product-subarray/description/
https://leetcode-cn.com/problems/house-robber/
2.https://leetcode-cn.com/problems/house-robber-ii/description/
3.https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/#/description
https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/
https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/
https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/
https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iv/
https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/
https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/solution/yi-ge-fang-fa-tuan-mie-6-dao-gu-piao-wen-ti-by-l-3/https://leetcode-cn.com/problems/perfect-squares/
2.https://leetcode-cn.com/problems/edit-distance/ (重点)
3.https://leetcode-cn.com/problems/jump-game/
4.https://leetcode-cn.com/problems/jump-game-ii/
5.https://leetcode-cn.com/problems/unique-paths/
6.https://leetcode-cn.com/problems/unique-paths-ii/
7. https://leetcode-cn.com/problems/unique-paths-iii/
8.https://leetcode-cn.com/problems/coin-change/
9.https://leetcode-cn.com/problems/coin-change-2/
5. HomeWork
1.https://leetcode-cn.com/problems/longest-valid-parentheses/
2.https://leetcode-cn.com/problems/minimum-path-sum/
3.https://leetcode-cn.com/problems/edit-distance/
4.https://leetcode-cn.com/problems/decode-ways
5.https://leetcode-cn.com/problems/maximal-square/
6.https://leetcode-cn.com/problems/max-sum-of-rectangle-no-larger-than-k/
7.https://leetcode-cn.com/problems/frog-jump/
8.https://leetcode-cn.com/problems/split-array-largest-sum
9.https://leetcode-cn.com/problems/student-attendance-record-ii/
10.https://leetcode-cn.com/problems/task-scheduler/
11.https://leetcode-cn.com/problems/palindromic-substrings/
12.https://leetcode-cn.com/problems/minimum-window-substring/
13.https://leetcode-cn.com/problems/burst-balloons/