-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
xiao-nx
committed
Sep 19, 2022
1 parent
2592f99
commit 2d25f55
Showing
3 changed files
with
279 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### 微软面试题\n", | ||
"https://www.nowcoder.com/discuss/954988" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"##### 一面\n", | ||
"给一个employ和manage的关系 \\\n", | ||
"|employ|manage|\n", | ||
"| :----: | :----: |\n", | ||
"| 2 | 5 |\n", | ||
"| 5 | 6 |\n", | ||
"| 1 | 6 |\n", | ||
"\n", | ||
"请把他按管理层级打印成如下json串的形式 \\\n", | ||
"[ \\\n", | ||
"{\\\n", | ||
"id:6\\\n", | ||
"sub:[\\\n", | ||
"{\\\n", | ||
"id:5\\\n", | ||
"sub:[\\\n", | ||
"{id:2}\\\n", | ||
"]\\\n", | ||
"}\\\n", | ||
"{id:1}\\\n", | ||
"]\\\n", | ||
"}\\\n", | ||
"]\\" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"##### Note: 是一道看似简单,实际简单,但是写起来并不容易的题。" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\">[</span>\n", | ||
" <span style=\"font-weight: bold\">{</span>\n", | ||
" <span style=\"color: #000080; text-decoration-color: #000080; font-weight: bold\">\"id\"</span>: <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">6</span>,\n", | ||
" <span style=\"color: #000080; text-decoration-color: #000080; font-weight: bold\">\"sub\"</span>: <span style=\"font-weight: bold\">[</span>\n", | ||
" <span style=\"font-weight: bold\">{</span>\n", | ||
" <span style=\"color: #000080; text-decoration-color: #000080; font-weight: bold\">\"id\"</span>: <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">5</span>,\n", | ||
" <span style=\"color: #000080; text-decoration-color: #000080; font-weight: bold\">\"sub\"</span>: <span style=\"font-weight: bold\">[</span>\n", | ||
" <span style=\"font-weight: bold\">{</span>\n", | ||
" <span style=\"color: #000080; text-decoration-color: #000080; font-weight: bold\">\"id\"</span>: <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">2</span>\n", | ||
" <span style=\"font-weight: bold\">}</span>\n", | ||
" <span style=\"font-weight: bold\">]</span>\n", | ||
" <span style=\"font-weight: bold\">}</span>,\n", | ||
" <span style=\"font-weight: bold\">{</span>\n", | ||
" <span style=\"color: #000080; text-decoration-color: #000080; font-weight: bold\">\"id\"</span>: <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>\n", | ||
" <span style=\"font-weight: bold\">}</span>\n", | ||
" <span style=\"font-weight: bold\">]</span>\n", | ||
" <span style=\"font-weight: bold\">}</span>\n", | ||
"<span style=\"font-weight: bold\">]</span>\n", | ||
"</pre>\n" | ||
], | ||
"text/plain": [ | ||
"\u001b[1m[\u001b[0m\n", | ||
" \u001b[1m{\u001b[0m\n", | ||
" \u001b[1;34m\"id\"\u001b[0m: \u001b[1;36m6\u001b[0m,\n", | ||
" \u001b[1;34m\"sub\"\u001b[0m: \u001b[1m[\u001b[0m\n", | ||
" \u001b[1m{\u001b[0m\n", | ||
" \u001b[1;34m\"id\"\u001b[0m: \u001b[1;36m5\u001b[0m,\n", | ||
" \u001b[1;34m\"sub\"\u001b[0m: \u001b[1m[\u001b[0m\n", | ||
" \u001b[1m{\u001b[0m\n", | ||
" \u001b[1;34m\"id\"\u001b[0m: \u001b[1;36m2\u001b[0m\n", | ||
" \u001b[1m}\u001b[0m\n", | ||
" \u001b[1m]\u001b[0m\n", | ||
" \u001b[1m}\u001b[0m,\n", | ||
" \u001b[1m{\u001b[0m\n", | ||
" \u001b[1;34m\"id\"\u001b[0m: \u001b[1;36m1\u001b[0m\n", | ||
" \u001b[1m}\u001b[0m\n", | ||
" \u001b[1m]\u001b[0m\n", | ||
" \u001b[1m}\u001b[0m\n", | ||
"\u001b[1m]\u001b[0m\n" | ||
] | ||
}, | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
} | ||
], | ||
"source": [ | ||
"from typing import Tuple, List, Dict, Any, Set\n", | ||
"import rich, json\n", | ||
"\n", | ||
"ids: Dict[int, List[int]] = {}\n", | ||
"\n", | ||
"def gen_dict(root: int) -> Dict[Any, Any]:\n", | ||
" sub: List[Dict[Any, Any]] = []\n", | ||
" for i in ids[root]:\n", | ||
" sub.append(gen_dict(i))\n", | ||
" ret: Dict[Any, Any] = {\n", | ||
" \"id\": root\n", | ||
" }\n", | ||
" if sub:\n", | ||
" ret[\"sub\"] = sub\n", | ||
" return ret\n", | ||
"\n", | ||
"def main(rel: List[Tuple[int, int]]):\n", | ||
" root: Set[int] = set()\n", | ||
" for employ, manager in rel:\n", | ||
" root.add(manager)\n", | ||
" root.discard(employ)\n", | ||
" if employ not in ids:\n", | ||
" ids[employ] = []\n", | ||
" if manager not in ids:\n", | ||
" ids[manager] = []\n", | ||
" if employ not in ids[manager]:\n", | ||
" ids[manager].append(employ)\n", | ||
" ret: List[Dict[Any, Any]] = []\n", | ||
" for id in root:\n", | ||
" ret.append(gen_dict(id))\n", | ||
" \n", | ||
" rich.print_json(json.dumps(ret))\n", | ||
"\n", | ||
"test = [(2, 5), (5, 6), (1, 6)]\n", | ||
"\n", | ||
"main(test)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3.6.7 64-bit (system)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.6.7" | ||
}, | ||
"orig_nbformat": 4, | ||
"vscode": { | ||
"interpreter": { | ||
"hash": "f4e9cda46bb2d9d7fe6ecdff0f8336a934348bf06cb492f2f42f60739b3403b4" | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import pandas as pd | ||
|
||
df = pd.read_json('train.json') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# LeetCode 动态规划题型 | ||
|
||
// reference: | ||
https://www.bilibili.com/video/BV1xb411e7ww?spm_id_from=333.337.search-card.all.click | ||
科技公司面试必考题 | ||
中上难度 | ||
必须掌握 | ||
### 1.如何学习动态规划? | ||
有规律可循,掌握思想,举一反三 | ||
### 2.动态规划题目特点 | ||
* 1.计数 | ||
-- 有多少种方式走到右下角 | ||
-- 有多少种方式选出K个数使得和是Sum | ||
* 2.求最大最小值 | ||
-- 从左上角走到右下角路径的最大数字和 | ||
-- 最长上升子序列长度 | ||
* 3.求存在性 | ||
-- 取石子游戏,先手是否必胜 | ||
-- 能不能选出K个数使得和是Sum | ||
|
||
### 3.动态规划组成部分 | ||
|
||
#### 1.确定状态 | ||
确定最后一步,即最优策略中的最后一枚硬币coins[k] | ||
转化成子问题,即最少的硬币拼出更小的面值amout-coins[k] | ||
|
||
#### 2.转移方程 | ||
根据子问题定义直接得到 | ||
f[X] = min{f[X-2]+1, f[X-5]+1, f[X-7]+1} | ||
|
||
#### 3.初始条件和边界情况 | ||
f[0] = 0 | ||
如果不能组合出amount, f[amount] = inf | ||
Note: 细心,考虑周全 | ||
|
||
#### 4.确定遍历顺序 | ||
利用之前的计算结果 | ||
f[0],f[1],f[2]...f[amount] | ||
### 常见动态规划类型 | ||
* 1.坐标型动态规划(20%) | ||
* 2.序列型动态规划(20%) | ||
* 3.划分型动态规划(20%) | ||
* 4.背包型动态规划 | ||
* 5.最长序列型动态规划 | ||
* 6.博弈型动态规划 | ||
* 7.综合性动态规划 | ||
### 动态规划时间空间优化 | ||
FollowUp常考 | ||
|
||
### 动态规划打印路径 | ||
|
||
### 动态规划初探 | ||
#### 坐标型动态规划 | ||
* Unique Path | ||
|
||
* Unique Path Ⅱ | ||
网格中有障碍 | ||
-- 1. 确定状态 | ||
最后一步一定是从左边(i,j-1)或者上边(i-1,j)过来 | ||
状态f[i][j]表示从左上角有多少种方式走到格子(i,j) | ||
-- 2.转移方程 | ||
坐标型动态规划中数组下标[i][j]即坐标(i,j) | ||
f[i][j] = f[i][j-1] + f[i-1][j] | ||
-- 3.初始条件和边界情况 | ||
左上角(0,0)或者右下角(m-1,n-1)有障碍,则无法到达,返回0 | ||
格子(i,j)有障碍,则f[i][j]=0表示无法到达此格子 | ||
|
||
$F[i][j]=\left\{\begin{matrix} | ||
0, & & {if(i,j)有障碍}\\ | ||
1, & & {i=0 且 j=0}\\ | ||
f[i-1][j], & & {j=1,第一列} \\ | ||
f[i][j-1], & & {i=1,第一行} \\ | ||
f[i-1][j] + f[i][j-1], & & {Others} | ||
\end{matrix}\right.$ | ||
|
||
#### 序列型动态规划 | ||
* Paint House | ||
-- 1. | ||
-- 2. | ||
-- 3. 初始条件和边界情况 | ||
设油漆前i栋房子并且房子i-1是红色、蓝色、绿色的最小花费分别为f[i][0],f[i][1],f[i][2] | ||
初始条件: | ||
f[0][0] = f[0][1] = f[0][2] = 0 | ||
即不漆任何房子的花费 | ||
-- 4. | ||
|
||
|
||
#### 划分型动态规划 | ||
|
||
|
||
下面是我们用Python代码实现的 | ||
```Python | ||
class Solution: | ||
def coinChange(self, coins, amount) -> int: | ||
dp = [float('inf')] * (amount + 1) | ||
# 初始条件 | ||
dp[0] = 0 | ||
# 确定遍历顺序 | ||
for i in range(1, amount + 1): | ||
# 最后一枚硬币coins[j] | ||
for j in range(len(coins)): | ||
# 边界条件 | ||
if coins[j] <= i and dp[i-coins[j] != float('inf')]: | ||
dp[i] = min(dp[i], dp[i - coins[j]] + 1) | ||
|
||
if dp[amount] == float('inf'): | ||
return -1 | ||
return dp[amount] | ||
``` |