From 051459a5426fc6ad44e069bbd7ab22b3f5d737b8 Mon Sep 17 00:00:00 2001 From: suinming <0223314338aa@gmail.com> Date: Tue, 27 Aug 2024 10:45:45 +0800 Subject: [PATCH 1/8] =?UTF-8?q?feat:=20=E5=8B=95=E6=85=8B=E8=A6=8F?= =?UTF-8?q?=E5=8A=83leetcode#714=EF=BC=8C=E6=96=B0=E5=A2=9Epython=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0714.买卖股票的最佳时机含手续费(动态规划).md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/problems/0714.买卖股票的最佳时机含手续费(动态规划).md b/problems/0714.买卖股票的最佳时机含手续费(动态规划).md index 73714147..b0e8b141 100644 --- a/problems/0714.买卖股票的最佳时机含手续费(动态规划).md +++ b/problems/0714.买卖股票的最佳时机含手续费(动态规划).md @@ -188,6 +188,20 @@ class Solution: return max(dp[-1][0], dp[-1][1]) ``` +```python +class Solution: + def maxProfit(self, prices: List[int], fee: int) -> int: + # 持有股票手上的最大現金 + hold = -prices[0] - fee + # 不持有股票手上的最大現金 + not_hold = 0 + for price in prices[1:]: + new_hold = max(hold, not_hold - price - fee) + new_not_hold = max(not_hold, hold + price) + hold, not_hold = new_hold, new_not_hold + return not_hold +``` + ### Go: ```go From c5686b417d24a1d999d34ba9cf29d85ce77fe602 Mon Sep 17 00:00:00 2001 From: Jasen <141482690+JasenWn@users.noreply.github.com> Date: Tue, 27 Aug 2024 22:32:59 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E6=9B=B4=E6=96=B00503.=E4=B8=8B=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0II=E7=9A=84python?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0503.下一个更大元素II.md | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/problems/0503.下一个更大元素II.md b/problems/0503.下一个更大元素II.md index 62066d85..5751bb91 100644 --- a/problems/0503.下一个更大元素II.md +++ b/problems/0503.下一个更大元素II.md @@ -168,6 +168,7 @@ class Solution { ``` ### Python: +> 版本一: ```python class Solution: @@ -181,6 +182,34 @@ class Solution: stack.append(i%len(nums)) return dp ``` + +> 版本二:针对版本一的优化 + +```python3 +class Solution: + def nextGreaterElements(self, nums: List[int]) -> List[int]: + res = [-1] * len(nums) + stack = [] + #第一次遍历nums + for i, num in enumerate(nums): + while stack and num > nums[stack[-1]]: + res[stack[-1]] = num + stack.pop() + stack.append(i) + #此时stack仍有剩余,有部分数‘无下一个更大元素’待修正 + #第二次遍历nums + for num in nums: + #一旦stack为空,就表明所有数都有下一个更大元素,可以返回结果 + if not stack: + return res + while stack and num > nums[stack[-1]]: + res[stack[-1]] = num + stack.pop() + #不要将已经有下一个更大元素的数加入栈,这样会重复赋值,只需对第一次遍历剩余的数再尝试寻找下一个更大元素即可 + #最后仍有部分最大数无法有下一个更大元素,返回结果 + return res +``` + ### Go: ```go @@ -203,7 +232,6 @@ func nextGreaterElements(nums []int) []int { return result } ``` - ### JavaScript: ```JS From 0846cd122e93e2472976164484d971990bd68ffa Mon Sep 17 00:00:00 2001 From: feobay Date: Fri, 30 Aug 2024 16:05:11 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A00309.=E6=9C=80=E4=BD=B3?= =?UTF-8?q?=E4=B9=B0=E5=8D=96=E8=82=A1=E7=A5=A8=E6=97=B6=E6=9C=BA=E5=90=AB?= =?UTF-8?q?=E5=86=B7=E5=86=BB=E6=9C=9FGo=E7=89=88=E6=9C=AC=E4=B8=80?= =?UTF-8?q?=E7=BB=B4=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0309.最佳买卖股票时机含冷冻期.md | 37 +++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/problems/0309.最佳买卖股票时机含冷冻期.md b/problems/0309.最佳买卖股票时机含冷冻期.md index 95689a48..4913b8bd 100644 --- a/problems/0309.最佳买卖股票时机含冷冻期.md +++ b/problems/0309.最佳买卖股票时机含冷冻期.md @@ -357,6 +357,42 @@ func max(a, b int) int { } ``` +```go +// 一维优化版本 +// 时间复杂度O(n), 空间复杂度O(1) +func maxProfit(prices []int) int { + + // 0: 持有,一直持有和买入 + // 1: 不持有,一直不持有(不包含前一天卖出,因为这样的一天是冷静期,状态有区别) + // 2:不持有,今天卖出 + // 3:冷静期,前一天卖出(一直不持有) + dp0, dp1, dp2, dp3 := -prices[0], 0, 0, 0 + + n := len(prices) + + for i := 1; i < n; i++ { + t0 := max(dp0, max(dp1, dp3)-prices[i]) + t1 := max(dp1, dp3) + t2 := dp0 + prices[i] + t3 := dp2 + + // 更新 + dp0, dp1, dp2, dp3 = t0, t1, t2, t3 + } + + return max(dp1, max(dp2, dp3)) +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` + + + ### Javascript: > 不同的状态定义 感觉更容易理解些 @@ -540,3 +576,4 @@ impl Solution { + From f38dc8aedc147c824b5eaa3621f399a623e9b681 Mon Sep 17 00:00:00 2001 From: feobay Date: Fri, 30 Aug 2024 16:19:40 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A00188.=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?IVGo=E7=89=88=E6=9C=AC=E7=A9=BA=E9=97=B4=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0188.买卖股票的最佳时机IV.md | 42 +++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/problems/0188.买卖股票的最佳时机IV.md b/problems/0188.买卖股票的最佳时机IV.md index def69277..0b1622ac 100644 --- a/problems/0188.买卖股票的最佳时机IV.md +++ b/problems/0188.买卖股票的最佳时机IV.md @@ -404,6 +404,47 @@ func max188(a, b int) int { } ``` +版本三:空间优化版本 + +```go +func maxProfit(k int, prices []int) int { + n := len(prices) + // k次交易,2 * k种状态 + // 状态从1开始计算,避免判断 + // 奇数时持有(保持或买入) + // 偶数时不持有(保持或卖出) + dp := make([][]int, 2) + dp[0] = make([]int, k * 2 + 1) + dp[1] = make([]int, k * 2 + 1) + + // 奇数状态时持有,i += 2 + for i := 1; i <= k * 2; i += 2 { + dp[0][i] = -prices[0] + } + + for i := 1; i < len(prices); i++ { + for j := 1; j <= k * 2; j++ { + if j % 2 == 1 { + dp[i % 2][j] = max(dp[(i - 1) % 2][j], dp[(i - 1) % 2][j - 1] - prices[i]) + } else { + dp[i % 2][j] = max(dp[(i - 1) % 2][j], dp[(i - 1) % 2][j - 1] + prices[i]) + } + } + } + + return dp[(n - 1) % 2][k * 2] +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` + + + ### JavaScript: ```javascript @@ -558,3 +599,4 @@ impl Solution { + From 830caef1222ddda1ab606f794c44e28e1be9b209 Mon Sep 17 00:00:00 2001 From: Leehouc <152672308+Leehouc@users.noreply.github.com> Date: Fri, 30 Aug 2024 21:06:47 +0800 Subject: [PATCH 5/8] =?UTF-8?q?Update=200044.=E5=BC=80=E5=8F=91=E5=95=86?= =?UTF-8?q?=E8=B4=AD=E4=B9=B0=E5=9C=9F=E5=9C=B0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/kamacoder/0044.开发商购买土地.md | 82 +++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/problems/kamacoder/0044.开发商购买土地.md b/problems/kamacoder/0044.开发商购买土地.md index 37bb98ed..ea2c696e 100644 --- a/problems/kamacoder/0044.开发商购买土地.md +++ b/problems/kamacoder/0044.开发商购买土地.md @@ -388,3 +388,85 @@ if __name__ == "__main__": main() ``` +### C + +前缀和 +```c +#include +#include + +int main() +{ + int n = 0, m = 0, ret_ver = 0, ret_hor = 0; + + // 读取行和列的值 + scanf("%d%d", &n, &m); + // 动态分配数组a(横)和b(纵)的空间 + int *a = (int *)malloc(sizeof(int) * n); + int *b = (int *)malloc(sizeof(int) * m); + + // 初始化数组a和b + for (int i = 0; i < n; i++) + { + a[i] = 0; + } + for (int i = 0; i < m; i++) + { + b[i] = 0; + } + + // 读取区块权值并计算每行和每列的总权值 + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + int tmp; + scanf("%d", &tmp); + a[i] += tmp; + b[j] += tmp; + } + } + + // 计算每列以及每行的前缀和 + for (int i = 1; i < n; i++) + { + a[i] += a[i - 1]; + } + for (int i = 1; i < m; i++) + { + b[i] += b[i - 1]; + } + + // 初始化ret_ver和ret_hor为最大可能值 + ret_hor = a[n - 1]; + ret_ver = b[m - 1]; + + // 计算按行划分的最小差异 + int ret2 = 0; + while (ret2 < n) + { + ret_hor = (ret_hor > abs(a[n - 1] - 2 * a[ret2])) ? abs(a[n - 1] - 2 * a[ret2]) : ret_hor; + // 原理同列,但更高级 + ret2++; + } + // 计算按列划分的最小差异 + int ret1 = 0; + while (ret1 < m) + { + if (ret_ver > abs(b[m - 1] - 2 * b[ret1])) + { + ret_ver = abs(b[m - 1] - 2 * b[ret1]); + } + ret1++; + } + + // 输出最小差异 + printf("%d\n", (ret_ver <= ret_hor) ? ret_ver : ret_hor); + + // 释放分配的内存 + free(a); + free(b); + return 0; +} + +``` From 2c52c3c363e13803ff9b46c16e9ec37ead8fe41c Mon Sep 17 00:00:00 2001 From: Leehouc <152672308+Leehouc@users.noreply.github.com> Date: Fri, 30 Aug 2024 21:31:45 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=2058.=20=E5=8C=BA?= =?UTF-8?q?=E9=97=B4=E5=92=8C=20C=E8=AF=AD=E8=A8=80=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/kamacoder/0058.区间和.md | 42 +++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/problems/kamacoder/0058.区间和.md b/problems/kamacoder/0058.区间和.md index f5ce08dc..c5a84a29 100644 --- a/problems/kamacoder/0058.区间和.md +++ b/problems/kamacoder/0058.区间和.md @@ -263,3 +263,45 @@ if __name__ == "__main__": main() ``` +### C + +```C +#include +#include + +int main(int argc, char *argv[]) +{ + int num; + // 读取数组长度 + scanf("%d", &num); + + // 使用动态内存分配而不是静态数组,以适应不同的输入大小 + int *a = (int *)malloc((num + 1) * sizeof(int)); + + // 初始化前缀和数组的第一个元素为0 + a[0] = 0; + + // 读取数组元素并计算前缀和 + for (int i = 1; i <= num; i++) + { + int mm; + scanf("%d", &mm); + // 累加前缀和 + a[i] = a[i - 1] + mm; + } + + int m, n; + // 循环读取区间并计算区间和,直到输入结束 + // scanf()返回成功匹配和赋值的个数,到达文件末尾则返回 EOF + while (scanf("%d%d", &m, &n) == 2) + { + // 输出区间和,注意区间是左闭右开,因此a[n+1]是包含n的元素的前缀和 + printf("%d\n", a[n+1] - a[m]); + } + + // 释放之前分配的内存 + free(a); + return 0; +} + +``` From 8dc91092774defa6ab621b1d50de1968df6cc7ba Mon Sep 17 00:00:00 2001 From: wangq635 <643797037@qq.com> Date: Sun, 1 Sep 2024 19:49:39 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E4=BF=AE=E6=94=B90701.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=8F=92=E5=85=A5?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=EF=BC=8C=E5=A2=9E=E5=8A=A0python=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E7=9A=84=E8=BF=AD=E4=BB=A3=E6=B3=95=EF=BC=88=E7=B2=BE?= =?UTF-8?q?=E7=AE=80=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0701.二叉搜索树中的插入操作.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0701.二叉搜索树中的插入操作.md b/problems/0701.二叉搜索树中的插入操作.md index 6b9e5834..c56a2624 100644 --- a/problems/0701.二叉搜索树中的插入操作.md +++ b/problems/0701.二叉搜索树中的插入操作.md @@ -370,6 +370,30 @@ class Solution: ``` + +迭代法(精简) +```python +class Solution: + def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]: + if not root: # 如果根节点为空,创建新节点作为根节点并返回 + return TreeNode(val) + cur = root + while cur: + if val < cur.val: + if not cur.left: # 如果此时父节点的左子树为空 + cur.left = TreeNode(val) # 将新节点连接到父节点的左子树 + return root + else: + cur = cur.left + elif val > cur.val: + if not cur.right: # 如果此时父节点的左子树为空 + cur.right = TreeNode(val) # 将新节点连接到父节点的右子树 + return root + else: + cur = cur.right + +``` + ----- ### Go From 75da0248bd134384cea412df2043b15bc0640631 Mon Sep 17 00:00:00 2001 From: wangq635 <643797037@qq.com> Date: Sun, 1 Sep 2024 20:22:42 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=200701.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=8F=92?= =?UTF-8?q?=E5=85=A5=E6=93=8D=E4=BD=9C=EF=BC=8C=E9=80=92=E5=BD=92=E6=B3=95?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E4=BA=8C=E5=BA=94=E8=AF=A5=E6=98=AF=E8=BF=AD?= =?UTF-8?q?=E4=BB=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0701.二叉搜索树中的插入操作.md | 50 ++++++++++++------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/problems/0701.二叉搜索树中的插入操作.md b/problems/0701.二叉搜索树中的插入操作.md index c56a2624..25d39486 100644 --- a/problems/0701.二叉搜索树中的插入操作.md +++ b/problems/0701.二叉搜索树中的插入操作.md @@ -283,32 +283,10 @@ class Solution: return TreeNode(val) self.traversal(root, val) return root - ``` 递归法(版本二) ```python -class Solution: - def insertIntoBST(self, root, val): - if root is None: - return TreeNode(val) - parent = None - cur = root - while cur: - parent = cur - if val < cur.val: - cur = cur.left - else: - cur = cur.right - if val < parent.val: - parent.left = TreeNode(val) - else: - parent.right = TreeNode(val) - return root -``` - -递归法(版本三) -```python class Solution: def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]: if root is None or root.val == val: @@ -326,7 +304,7 @@ class Solution: return root ``` -递归法(版本四) +递归法(版本三) ```python class Solution: def insertIntoBST(self, root, val): @@ -340,10 +318,9 @@ class Solution: root.right = self.insertIntoBST(root.right, val) return root - ``` -迭代法 +迭代法(版本一) ```python class Solution: def insertIntoBST(self, root, val): @@ -366,9 +343,28 @@ class Solution: else: parent.right = node # 将新节点连接到父节点的右子树 - return root + return root +``` - +迭代法(版本二) +```python +class Solution: + def insertIntoBST(self, root, val): + if root is None: + return TreeNode(val) + parent = None + cur = root + while cur: + parent = cur + if val < cur.val: + cur = cur.left + else: + cur = cur.right + if val < parent.val: + parent.left = TreeNode(val) + else: + parent.right = TreeNode(val) + return root ``` 迭代法(精简)