From 42601ef482e138c05ad4ffe8f94026cf0665587f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B4=94=E6=B0=B8=E5=87=A1?= <3141722654@qq.com> Date: Sun, 21 May 2023 17:51:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B4=94=E6=B0=B8=E5=87=A1=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- alg.md | 39 --------------------------- alg.py | 0 alg1.md | 59 +++++++++++++++++++++++++++++++++++++++++ alg1.py | 10 +++++++ alg2.md | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ alg2.py | 23 ++++++++++++++++ alg3.md | 58 ++++++++++++++++++++++++++++++++++++++++ alg3.py | 7 +++++ gen.py | 0 gen1.py | 10 +++++++ gen2.py | 8 ++++++ gen3.py | 13 +++++++++ test.py | 36 ------------------------- test1.py | 10 +++++++ test2.py | 1 + test3.py | 2 ++ 16 files changed, 282 insertions(+), 75 deletions(-) delete mode 100644 alg.md delete mode 100644 alg.py create mode 100644 alg1.md create mode 100644 alg1.py create mode 100644 alg2.md create mode 100644 alg2.py create mode 100644 alg3.md create mode 100644 alg3.py delete mode 100644 gen.py create mode 100644 gen1.py create mode 100644 gen2.py create mode 100644 gen3.py delete mode 100644 test.py create mode 100644 test1.py create mode 100644 test2.py create mode 100644 test3.py diff --git a/alg.md b/alg.md deleted file mode 100644 index 5e691e6..0000000 --- a/alg.md +++ /dev/null @@ -1,39 +0,0 @@ -# 算法设计与分析第四次作业 - -#### 介绍 -{**以下是 Gitee 平台说明,您可以替换此简介** -Gitee 是 OSCHINA 推出的基于 Git 的代码托管平台(同时支持 SVN)。专为开发者提供稳定、高效、安全的云端软件开发协作平台 -无论是个人、团队、或是企业,都能够用 Gitee 实现代码托管、项目管理、协作开发。企业项目请看 [https://gitee.com/enterprises](https://gitee.com/enterprises)} - -#### 软件架构 -软件架构说明 - - -#### 安装教程 - -1. xxxx -2. xxxx -3. xxxx - -#### 使用说明 - -1. xxxx -2. xxxx -3. xxxx - -#### 参与贡献 - -1. Fork 本仓库 -2. 新建 Feat_xxx 分支 -3. 提交代码 -4. 新建 Pull Request - - -#### 特技 - -1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md -2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) -3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 -4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 -5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) -6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) diff --git a/alg.py b/alg.py deleted file mode 100644 index e69de29..0000000 diff --git a/alg1.md b/alg1.md new file mode 100644 index 0000000..90fd1f1 --- /dev/null +++ b/alg1.md @@ -0,0 +1,59 @@ +# 算法设计第四次作业 + +### 根据身高重建队列 + +1.问题描述 +给定一个数组 `people`,其中 `people[i]` 表示第 `i` 个人的身高为 `people[i][0]`,前面正好有 `people[i][1]` 个身高大于或等于 `people[i][0]` 的人。 +请你按照身高从高到低重新排列 `people` 数组,使得每个人重新排列后的位置与其原来的位置相同,并且前面只有比他高的人或和他身高相同的人。 + +2.*思路*:首先对数对进行排序,按照数对的元素 1 降序排序,按照数对的元素 2 升序排序。原因是,按照元素 1 进行降序排序,对于每个元素,在其之前的元素的个数,就是大于等于他的元素的数量,而按照第二个元素正向排序,我们希望 k 大的尽量在后面,减少插入操作的次数。 + +```python +class Solution: + def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]: + res = [] + people = sorted(people, key = lambda x: (-x[0], x[1])) + for p in people: + if len(res) <= p[1]: + res.append(p) + elif len(res) > p[1]: + res.insert(p[1], p) + return res +``` + + + +###### 3.性能分析 + + + +**空间性能**: +$$ +O(n) +$$ +因为代码中需要使用一个列表来存储插入之后的队列,而队列的长度为 n,所以空间复杂度为 O(n)。 + +**时间性能**: +$$ +O(n^2) +$$ + +其中 n是数组 `people` 的长度。因为代码中使用了一个循环遍历所有的人,并根据每个人的k值将其插入到对应的位置上。由于需要不断地插入元素,导致时间复杂度为 O(n^2) + +###### 4.代码测试 + +![](C:\Users\崔\Pictures\Screenshots\屏幕截图 2023-05-19 142401.png) + +###### 5.心得体会 + +git使用愈发熟悉,在文件的创建和对文件的提交上掌握程度提高,不会的问题及时询问同学解决。 + +###### 6.实验环境 + +win11. + +python3.11 + +vs2022 + +git. diff --git a/alg1.py b/alg1.py new file mode 100644 index 0000000..8674a6e --- /dev/null +++ b/alg1.py @@ -0,0 +1,10 @@ +class Solution: + def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]: + res = [] + people = sorted(people, key = lambda x: (-x[0], x[1])) + for p in people: + if len(res) <= p[1]: + res.append(p) + elif len(res) > p[1]: + res.insert(p[1], p) + return res \ No newline at end of file diff --git a/alg2.md b/alg2.md new file mode 100644 index 0000000..a4d3b2b --- /dev/null +++ b/alg2.md @@ -0,0 +1,81 @@ +# 算法设计第四次作业 + +### 分割回文串 + +1.问题描述给你一个字符串 `s`,请你将 `s` 分割成一些子串,使每个子串都是 **回文串** 。返回 `s` 所有可能的分割方案。 + +**回文串** 是正着读和反着读都一样的字符串。 + +2.*思路*: + +确定状态:定义状态 dp[i] 表示字符串 s中前 i 个字符可以被分割成的最小回文串数量。 + +转移方程:对于一个回文串 s[j:i],若 s[j:i] 是回文串,则可以将其作为一个独立的回文串,此时状态转移方程为 + +dp[i] = min(dp[i], dp[j-1]+1),其中 dp[j-1]+1 表示将 s[j:i] 作为一个独立的回文串,所需的最小回文串数量。 + +初始状态:dp[0]=0,即空字符串的最小回文串数量为 0。 + 计算顺序:由于转移方程需要用到 dp[j-1],因此需要先计算出 dp[0], dp[1], ..., dp[j-1] 的值,再计算 dp[j], dp[j+1], ..., dp[n] 的值。 + 最终结果:最终答案为 dp[n],其中 n 为字符串 s 的长度。 + +```python +class Solution: + def partition(self, s: str) -> List[List[str]]: + res = [] + n = len(s) + # dp[i][j] 表示 s[i:j+1] 是否为回文串 + dp = [[False] * n for _ in range(n)] + for i in range(n): + dp[i][i] = True + for j in range(i): + if s[i] == s[j] and (i - j <= 2 or dp[j+1][i-1]): + dp[j][i] = True + path = [] + def dfs(start): + if start == n: + res.append(path[:]) + return + for i in range(start, n): + if dp[start][i]: + path.append(s[start:i+1]) + dfs(i+1) + path.pop() + dfs(0) + return res + +``` + + + +###### 3.性能分析 + + + +**空间性能**: +$$ +O(n^2) +$$ +其中 n 是字符串 `s` 的长度。需要 $O(n^2)$ 的空间存储 `dp` 数组,以及 O(n) 的栈空间用于 `dfs` 函数的递归调用。因此总空间复杂度为 O(n^2)。 + +**时间性能**: +$$ +O(n^2+2^n) +$$ + +其中 n 是字符串 `s` 的长度。需要 O(n^2) 的时间计算 `dp` 数组,然后需要 O(2^n) 的时间枚举所有的分割方案。因此总时间复杂度为 O(n^2+2^n)。 + +###### 4.代码测试 + +![](C:\Users\崔\Pictures\Screenshots\屏幕截图 2023-05-19 144107.png) + + + +###### 5.实验环境 + +win11. + +python3.11 + +vs2022 + +git. diff --git a/alg2.py b/alg2.py new file mode 100644 index 0000000..433a3c1 --- /dev/null +++ b/alg2.py @@ -0,0 +1,23 @@ +class Solution: + def partition(self, s: str) -> List[List[str]]: + res = [] + n = len(s) + # dp[i][j] 表示 s[i:j+1] 是否为回文串 + dp = [[False] * n for _ in range(n)] + for i in range(n): + dp[i][i] = True + for j in range(i): + if s[i] == s[j] and (i - j <= 2 or dp[j+1][i-1]): + dp[j][i] = True + path = [] + def dfs(start): + if start == n: + res.append(path[:]) + return + for i in range(start, n): + if dp[start][i]: + path.append(s[start:i+1]) + dfs(i+1) + path.pop() + dfs(0) + return res diff --git a/alg3.md b/alg3.md new file mode 100644 index 0000000..020b0bb --- /dev/null +++ b/alg3.md @@ -0,0 +1,58 @@ +# 算法设计第四次作业 + +### 不同路径 + +1.问题描述 +一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 “Start” )。 + +机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish” )。 + +问总共有多少条不同的路径? + +2.*思路*: + +确定状态:定义状态 dp[i][j]表示从起点到达坐标为 $(i,j)$ 的格子的不同路径数量。 +转移方程:由于只能向下或向右走,因此到达格子 $(i,j)$ 的路径数量为到达格子 (i-1,j) 和 (i,j-1) 的路径数量之和,即 dp[i][j] = dp[i-1][j] + dp[i][j-1]。 +初始状态:第一行和第一列的格子只能向右或向下走,因此到达这些格子的路径数量都为 1,即 dp[0][j] = dp[i][0] = 1。 计算顺序:由于转移方程需要用到 dp[i-1][j] 和 dp[i][j-1],因此需要从第二行和第二列开始计算,依次计算 dp[2][2], dp[2][3], ..., dp[2][n], dp[3][2], dp[3][3], ..., dp[m][n]的值。 +最终结果:最终答案为 dp[m][n],其中 m 和 n 分别为网格的行数和列数。 + +```python +class Solution: + def uniquePaths(self, m: int, n: int) -> int: + dp = [[1] * n for _ in range(m)] # 初始化为 1 + for i in range(1, m): + for j in range(1, n): + dp[i][j] = dp[i-1][j] + dp[i][j-1] + return dp[m-1][n-1] +``` + + + +###### 3.性能分析 + + + +**空间性能**: +$$ +O(n) +$$ +**时间性能**: +$$ +O(mn) +$$ + +###### 4.代码测试 + +![](C:\Users\崔\Pictures\Screenshots\屏幕截图 2023-05-19 145711.png) + + + +###### 5.实验环境 + +win11. + +python3.11 + +vs2022 + +git. diff --git a/alg3.py b/alg3.py new file mode 100644 index 0000000..9ab3fd6 --- /dev/null +++ b/alg3.py @@ -0,0 +1,7 @@ +class Solution: + def uniquePaths(self, m: int, n: int) -> int: + dp = [[1] * n for _ in range(m)] # 初始化为 1 + for i in range(1, m): + for j in range(1, n): + dp[i][j] = dp[i-1][j] + dp[i][j-1] + return dp[m-1][n-1] \ No newline at end of file diff --git a/gen.py b/gen.py deleted file mode 100644 index e69de29..0000000 diff --git a/gen1.py b/gen1.py new file mode 100644 index 0000000..d4ca40f --- /dev/null +++ b/gen1.py @@ -0,0 +1,10 @@ +import random +n = 10 # 数组长度 +max_height = 200 # 最大身高 +max_k = n # 最大k值 +people = [] +for i in range(n): + height = random.randint(1, max_height) + k = random.randint(0, max_k) + people.append([height, k]) +print(people) \ No newline at end of file diff --git a/gen2.py b/gen2.py new file mode 100644 index 0000000..1cae8ae --- /dev/null +++ b/gen2.py @@ -0,0 +1,8 @@ +import random +import string +def generate_data(n): + s = ''.join(random.choice(string.ascii_lowercase) for _ in range(n)) + return s +n = 10 +s = generate_data(n) +print(s) \ No newline at end of file diff --git a/gen3.py b/gen3.py new file mode 100644 index 0000000..a608718 --- /dev/null +++ b/gen3.py @@ -0,0 +1,13 @@ +import random +def generate_data(): + m, n = random.randint(1, 10), random.randint(1, 10) + return m, n +def run_test(m, n): + dp = [[1] * n for _ in range(m)] + for i in range(1, m): + for j in range(1, n): + dp[i][j] = dp[i-1][j] + dp[i][j-1] + return dp[m-1][n-1] +m, n = generate_data() +print("m={}, n={}".format(m, n)) +print("Result:", run_test(m, n)) \ No newline at end of file diff --git a/test.py b/test.py deleted file mode 100644 index b0f1d0b..0000000 --- a/test.py +++ /dev/null @@ -1,36 +0,0 @@ -# 算法设计与分析第四次作业 - -#### Description -{**When you're done, you can delete the content in this README and update the file with details for others getting started with your repository**} - -#### Software Architecture -Software architecture description - -#### Installation - -1. xxxx -2. xxxx -3. xxxx - -#### Instructions - -1. xxxx -2. xxxx -3. xxxx - -#### Contribution - -1. Fork the repository -2. Create Feat_xxx branch -3. Commit your code -4. Create Pull Request - - -#### Gitee Feature - -1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md -2. Gitee blog [blog.gitee.com](https://blog.gitee.com) -3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore) -4. The most valuable open source project [GVP](https://gitee.com/gvp) -5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help) -6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) diff --git a/test1.py b/test1.py new file mode 100644 index 0000000..0d51413 --- /dev/null +++ b/test1.py @@ -0,0 +1,10 @@ +import random +n = 10 # 数组长度 +max_height = 200 # 最大身高 +max_k = n # 最大k值 +people = [] +for i in range(n): + height = random.randint(1, max_height) + k = random.randint(0, max_k) + people.append([height, k]) +print(people) diff --git a/test2.py b/test2.py new file mode 100644 index 0000000..e5fc19d --- /dev/null +++ b/test2.py @@ -0,0 +1 @@ +tqjpusyvcm \ No newline at end of file diff --git a/test3.py b/test3.py new file mode 100644 index 0000000..fc9cf99 --- /dev/null +++ b/test3.py @@ -0,0 +1,2 @@ +m=5, n=4 +Result: 35 -- Gitee