From 4e1590e26db6363af09e0feef0109d27d48b3958 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 11:14:10 +0800 Subject: [PATCH 01/16] =?UTF-8?q?update=20=200474.=E4=B8=80=E5=92=8C?= =?UTF-8?q?=E9=9B=B6=20=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0474.一和零.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/problems/0474.一和零.md b/problems/0474.一和零.md index 8d79d701..6a178a25 100644 --- a/problems/0474.一和零.md +++ b/problems/0474.一和零.md @@ -156,6 +156,11 @@ public: }; ``` +* 时间复杂度: O(kmn),k 为strs的长度 +* 空间复杂度: O(mn) + + + ## 总结 不少同学刷过这道题,可能没有总结这究竟是什么背包。 From f701133a9a8b418904cf02583e90d3c7fcbdd743 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 11:17:04 +0800 Subject: [PATCH 02/16] =?UTF-8?q?update=20=20=200518.=E9=9B=B6=E9=92=B1?= =?UTF-8?q?=E5=85=91=E6=8D=A2II=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0518.零钱兑换II.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/problems/0518.零钱兑换II.md b/problems/0518.零钱兑换II.md index eb5a844c..c208754f 100644 --- a/problems/0518.零钱兑换II.md +++ b/problems/0518.零钱兑换II.md @@ -179,6 +179,11 @@ public: } }; ``` + +* 时间复杂度: O(mn),其中 m 是amount,n 是 coins 的长度 +* 空间复杂度: O(m) + + 是不是发现代码如此精简,哈哈 ## 总结 From 6e62049cd3f06e5bf156240087d90bdfb5910569 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 11:20:07 +0800 Subject: [PATCH 03/16] =?UTF-8?q?update=20=200377.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E6=80=BB=E5=92=8C=E2=85=A3=20=EF=BC=9A=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0377.组合总和Ⅳ.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/problems/0377.组合总和Ⅳ.md b/problems/0377.组合总和Ⅳ.md index f2287188..ee659723 100644 --- a/problems/0377.组合总和Ⅳ.md +++ b/problems/0377.组合总和Ⅳ.md @@ -130,6 +130,11 @@ public: ``` +* 时间复杂度: O(target * n),其中 n 为 nums 的长度 +* 空间复杂度: O(target) + + + C++测试用例有两个数相加超过int的数据,所以需要在if里加上dp[i] < INT_MAX - dp[i - num]。 但java就不用考虑这个限制,java里的int也是四个字节吧,也有可能leetcode后台对不同语言的测试数据不一样。 From 84b84e5bf11158c3ab4ac2356c8290060b7ebf45 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 11:46:51 +0800 Subject: [PATCH 04/16] =?UTF-8?q?update=20=20=200070.=E7=88=AC=E6=A5=BC?= =?UTF-8?q?=E6=A2=AF=E5=AE=8C=E5=85=A8=E8=83=8C=E5=8C=85=E7=89=88=E6=9C=AC?= =?UTF-8?q?=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86?= =?UTF-8?q?=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0070.爬楼梯完全背包版本.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/problems/0070.爬楼梯完全背包版本.md b/problems/0070.爬楼梯完全背包版本.md index 41c2e616..8f8bc9a6 100644 --- a/problems/0070.爬楼梯完全背包版本.md +++ b/problems/0070.爬楼梯完全背包版本.md @@ -101,6 +101,11 @@ public: }; ``` +* 时间复杂度: O(nm) +* 空间复杂度: O(n) + + + 代码中m表示最多可以爬m个台阶,代码中把m改成2就是本题70.爬楼梯可以AC的代码了。 ## 总结 From 9fef5cbd00bde88126d4407c913d4b5f448d5308 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 11:51:41 +0800 Subject: [PATCH 05/16] =?UTF-8?q?=20update=20=20=200322.=E9=9B=B6=E9=92=B1?= =?UTF-8?q?=E5=85=91=E6=8D=A2=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0322.零钱兑换.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/problems/0322.零钱兑换.md b/problems/0322.零钱兑换.md index 3be72565..0e3947da 100644 --- a/problems/0322.零钱兑换.md +++ b/problems/0322.零钱兑换.md @@ -133,6 +133,11 @@ public: }; ``` +* 时间复杂度: O(n * amount),其中 n 为 coins 的长度 +* 空间复杂度: O(amount) + + + 对于遍历方式遍历背包放在外循环,遍历物品放在内循环也是可以的,我就直接给出代码了 ```CPP @@ -154,6 +159,8 @@ public: } }; ``` +* 同上 + ## 总结 From b6fcdc160d85cd5d541c851f514e7ea332ffd4b4 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 11:57:47 +0800 Subject: [PATCH 06/16] =?UTF-8?q?=20update=20=20=200279.=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E5=B9=B3=E6=96=B9=E6=95=B0=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0279.完全平方数.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/problems/0279.完全平方数.md b/problems/0279.完全平方数.md index c329156b..f5b23d26 100644 --- a/problems/0279.完全平方数.md +++ b/problems/0279.完全平方数.md @@ -127,6 +127,10 @@ public: }; ``` +* 时间复杂度: O(n * √n) +* 空间复杂度: O(n) + + 同样我在给出先遍历物品,在遍历背包的代码,一样的可以AC的。 ```CPP @@ -145,6 +149,8 @@ public: } }; ``` +* 同上 + ## 总结 From ecf8251c6ad8c3524ebbe282a62a682acc91824b Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 13:58:53 +0800 Subject: [PATCH 07/16] =?UTF-8?q?update=20=20=200198.=E6=89=93=E5=AE=B6?= =?UTF-8?q?=E5=8A=AB=E8=88=8D=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0198.打家劫舍.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/problems/0198.打家劫舍.md b/problems/0198.打家劫舍.md index fdb2dabf..c25f3b86 100644 --- a/problems/0198.打家劫舍.md +++ b/problems/0198.打家劫舍.md @@ -108,6 +108,9 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(n) + ## 总结 打家劫舍是DP解决的经典题目,这道题也是打家劫舍入门级题目,后面我们还会变种方式来打劫的。 From 6754a955688dd80870593da89fa26d889074e748 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 14:00:01 +0800 Subject: [PATCH 08/16] =?UTF-8?q?update=20=200213.=E6=89=93=E5=AE=B6?= =?UTF-8?q?=E5=8A=AB=E8=88=8DII=20=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0213.打家劫舍II.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/problems/0213.打家劫舍II.md b/problems/0213.打家劫舍II.md index 0627eedb..becad069 100644 --- a/problems/0213.打家劫舍II.md +++ b/problems/0213.打家劫舍II.md @@ -82,6 +82,11 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(n) + + + ## 总结 成环之后还是难了一些的, 不少题解没有把“考虑房间”和“偷房间”说清楚。 From 56b35274356fe0cecda151e5d1aa77a9f8b750b3 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 14:07:01 +0800 Subject: [PATCH 09/16] =?UTF-8?q?update=20=200188.=E4=B9=B0=E5=8D=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?IV=20=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6?= =?UTF-8?q?=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0188.买卖股票的最佳时机IV.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/problems/0188.买卖股票的最佳时机IV.md b/problems/0188.买卖股票的最佳时机IV.md index 4fdd7bf4..f6744a2b 100644 --- a/problems/0188.买卖股票的最佳时机IV.md +++ b/problems/0188.买卖股票的最佳时机IV.md @@ -156,6 +156,11 @@ public: }; ``` +* 时间复杂度: O(n * k),其中 n 为 prices 的长度 +* 空间复杂度: O(n * k) + + + 当然有的解法是定义一个三维数组dp[i][j][k],第i天,第j次买卖,k表示买还是卖的状态,从定义上来讲是比较直观。 但感觉三维数组操作起来有些麻烦,我是直接用二维数组来模拟三维数组的情况,代码看起来也清爽一些。 From 5bc0fa5c96684068bec235ca353e297b199f5414 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 14:08:42 +0800 Subject: [PATCH 10/16] =?UTF-8?q?=20update=20=20=200300.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E4=B8=8A=E5=8D=87=E5=AD=90=E5=BA=8F=E5=88=97=EF=BC=9A=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0300.最长上升子序列.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/problems/0300.最长上升子序列.md b/problems/0300.最长上升子序列.md index 478837cc..e8cb0b5f 100644 --- a/problems/0300.最长上升子序列.md +++ b/problems/0300.最长上升子序列.md @@ -106,6 +106,10 @@ public: } }; ``` +* 时间复杂度: O(n^2) +* 空间复杂度: O(n) + + ## 总结 From 1ed179e233bc18574a3f4e7b46cbbed9ab686667 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 14:11:04 +0800 Subject: [PATCH 11/16] =?UTF-8?q?update=20=20=201143.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E5=85=AC=E5=85=B1=E5=AD=90=E5=BA=8F=E5=88=97=EF=BC=9A=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/1143.最长公共子序列.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/problems/1143.最长公共子序列.md b/problems/1143.最长公共子序列.md index 8e5f7eb2..730e9ad1 100644 --- a/problems/1143.最长公共子序列.md +++ b/problems/1143.最长公共子序列.md @@ -124,6 +124,10 @@ public: } }; ``` +* 时间复杂度: O(n * m),其中 n 和 m 分别为 text1 和 text2 的长度 +* 空间复杂度: O(n * m) + + ## 其他语言版本 From 4ded4b5c820f3098ab54a3dbd6cda23e0c5e70ff Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 14:12:41 +0800 Subject: [PATCH 12/16] =?UTF-8?q?update=20=20=201035.=E4=B8=8D=E7=9B=B8?= =?UTF-8?q?=E4=BA=A4=E7=9A=84=E7=BA=BF=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/1035.不相交的线.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/problems/1035.不相交的线.md b/problems/1035.不相交的线.md index d1e1e30a..7b60abdd 100644 --- a/problems/1035.不相交的线.md +++ b/problems/1035.不相交的线.md @@ -62,6 +62,10 @@ public: } }; ``` +* 时间复杂度: O(n * m) +* 空间复杂度: O(n * m) + + ## 总结 From f579a9f82e89f18338b7af64f94e76a63fa7f96d Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 14:14:03 +0800 Subject: [PATCH 13/16] =?UTF-8?q?update=20=20=200115.=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E7=9A=84=E5=AD=90=E5=BA=8F=E5=88=97=EF=BC=9A=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0115.不同的子序列.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/problems/0115.不同的子序列.md b/problems/0115.不同的子序列.md index daa708bc..6127f190 100644 --- a/problems/0115.不同的子序列.md +++ b/problems/0115.不同的子序列.md @@ -149,6 +149,11 @@ public: }; ``` +* 时间复杂度: O(n * m) +* 空间复杂度: O(n * m) + + + ## 其他语言版本 From 7d6476c16a421ef03c56ee101e1fd16b1d47ea9c Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 14:15:40 +0800 Subject: [PATCH 14/16] =?UTF-8?q?update=20=200583.=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=9A=84=E5=88=A0=E9=99=A4=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=20=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0583.两个字符串的删除操作.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/problems/0583.两个字符串的删除操作.md b/problems/0583.两个字符串的删除操作.md index eb046a9d..561ad2f2 100644 --- a/problems/0583.两个字符串的删除操作.md +++ b/problems/0583.两个字符串的删除操作.md @@ -104,6 +104,10 @@ public: }; ``` +* 时间复杂度: O(n * m) +* 空间复杂度: O(n * m) + + ### 动态规划二 @@ -127,6 +131,10 @@ public: }; ``` +* 时间复杂度: O(n * m) +* 空间复杂度: O(n * m) + + ## 其他语言版本 From 541ac6ef3cfe7da2312df2cedf9df63edce2b325 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 14:16:43 +0800 Subject: [PATCH 15/16] =?UTF-8?q?update=20=20=200072.=E7=BC=96=E8=BE=91?= =?UTF-8?q?=E8=B7=9D=E7=A6=BB=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0072.编辑距离.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/problems/0072.编辑距离.md b/problems/0072.编辑距离.md index 0b49e3c6..cc4ab00c 100644 --- a/problems/0072.编辑距离.md +++ b/problems/0072.编辑距离.md @@ -218,6 +218,10 @@ public: } }; ``` +* 时间复杂度: O(n * m) +* 空间复杂度: O(n * m) + + ## 其他语言版本 From a26fe0fdf4559c535ac3f04d4e43e87b86b01196 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 14:17:50 +0800 Subject: [PATCH 16/16] =?UTF-8?q?update=20=200516.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E5=9B=9E=E6=96=87=E5=AD=90=E5=BA=8F=E5=88=97=20=EF=BC=9A?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0516.最长回文子序列.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/problems/0516.最长回文子序列.md b/problems/0516.最长回文子序列.md index d28a33cc..fcdd57b0 100644 --- a/problems/0516.最长回文子序列.md +++ b/problems/0516.最长回文子序列.md @@ -144,6 +144,11 @@ public: } }; ``` +* 时间复杂度: O(n^2) +* 空间复杂度: O(n^2) + + + ## 其他语言版本