From 704c3bb7960fc40b3000d36e7ec6266747a9cc87 Mon Sep 17 00:00:00 2001 From: ironartisan Date: Wed, 18 Aug 2021 22:33:42 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A00084.=E6=9F=B1=E7=8A=B6?= =?UTF-8?q?=E5=9B=BE=E4=B8=AD=E6=9C=80=E5=A4=A7=E7=9A=84=E7=9F=A9=E5=BD=A2?= =?UTF-8?q?java=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0084.柱状图中最大的矩形.md | 34 ++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/problems/0084.柱状图中最大的矩形.md b/problems/0084.柱状图中最大的矩形.md index 941888db..bec3bc99 100644 --- a/problems/0084.柱状图中最大的矩形.md +++ b/problems/0084.柱状图中最大的矩形.md @@ -195,6 +195,40 @@ public: Java: +动态规划 +```java +class Solution { + public int largestRectangleArea(int[] heights) { + int length = heights.length; + int[] minLeftIndex = new int [length]; + int[] maxRigthIndex = new int [length]; + + // 记录左边第一个小于该柱子的下标 + minLeftIndex[0] = -1 ; + for (int i = 1; i < length; i++) { + int t = i - 1; + // 这里不是用if,而是不断向右寻找的过程 + while (t >= 0 && heights[t] >= heights[i]) t = minLeftIndex[t]; + minLeftIndex[i] = t; + } + // 记录每个柱子 右边第一个小于该柱子的下标 + maxRigthIndex[length - 1] = length; + for (int i = length - 2; i >= 0; i--) { + int t = i + 1; + while(t < length && heights[t] >= heights[i]) t = maxRigthIndex[t]; + maxRigthIndex[i] = t; + } + // 求和 + int result = 0; + for (int i = 0; i < length; i++) { + int sum = heights[i] * (maxRigthIndex[i] - minLeftIndex[i] - 1); + result = Math.max(sum, result); + } + return result; + } +} +``` + Python: 动态规划 From 321781cac57578a6c78b5276933828580194d57b Mon Sep 17 00:00:00 2001 From: ironartisan Date: Thu, 19 Aug 2021 14:46:15 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A00279.=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E5=B9=B3=E6=96=B9=E6=95=B0python3=E7=89=88=E6=9C=AC=E4=B8=80?= =?UTF-8?q?=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0279.完全平方数.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/problems/0279.完全平方数.md b/problems/0279.完全平方数.md index e51f0a99..863bd60c 100644 --- a/problems/0279.完全平方数.md +++ b/problems/0279.完全平方数.md @@ -217,6 +217,22 @@ class Solution: Python3: ```python class Solution: + '''版本一,先遍历背包, 再遍历物品''' + def numSquares(self, n: int) -> int: + dp = [n] * (n + 1) + dp[0] = 0 + # 遍历背包 + for j in range(1, n+1): + for i in range(1, n): + num = i ** 2 + if num > j: break + # 遍历物品 + if j - num >= 0: + dp[j] = min(dp[j], dp[j - num] + 1) + return dp[n] + +class Solution: + '''版本二, 先遍历物品, 再遍历背包''' def numSquares(self, n: int) -> int: # 初始化 # 组成和的完全平方数的最多个数,就是只用1构成 From aa4ce0f096013a20847b4b7c325a22fbe26e6deb Mon Sep 17 00:00:00 2001 From: ironartisan Date: Thu, 19 Aug 2021 15:03:44 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A00279.=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E5=B9=B3=E6=96=B9=E6=95=B0java=E7=89=88=E6=9C=AC=E4=BA=8C?= =?UTF-8?q?=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0279.完全平方数.md | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/problems/0279.完全平方数.md b/problems/0279.完全平方数.md index 863bd60c..f23453da 100644 --- a/problems/0279.完全平方数.md +++ b/problems/0279.完全平方数.md @@ -161,6 +161,7 @@ public: Java: ```Java class Solution { + // 版本一,先遍历物品, 再遍历背包 public int numSquares(int n) { int max = Integer.MAX_VALUE; int[] dp = new int[n + 1]; @@ -170,7 +171,9 @@ class Solution { } //当和为0时,组合的个数为0 dp[0] = 0; + // 遍历物品 for (int i = 1; i * i <= n; i++) { + // 遍历背包 for (int j = i * i; j <= n; j++) { if (dp[j - i * i] != max) { dp[j] = Math.min(dp[j], dp[j - i * i] + 1); @@ -180,6 +183,28 @@ class Solution { return dp[n]; } } + +class Solution { + // 版本二, 先遍历背包, 再遍历物品 + public int numSquares(int n) { + int max = Integer.MAX_VALUE; + int[] dp = new int[n + 1]; + // 初始化 + for (int j = 0; j <= n; j++) { + dp[j] = max; + } + // 当和为0时,组合的个数为0 + dp[0] = 0; + // 遍历背包 + for (int j = 1; j <= n; j++) { + // 遍历物品 + for (int i = 1; i * i <= j; i++) { + dp[j] = Math.min(dp[j], dp[j - i * i] + 1); + } + } + return dp[n]; + } +} ``` Python: @@ -187,7 +212,7 @@ Python: ```python3 class Solution: def numSquares(self, n: int) -> int: - '''版本一''' + '''版本一,先遍历背包, 再遍历物品''' # 初始化 nums = [i**2 for i in range(1, n + 1) if i**2 <= n] dp = [10**4]*(n + 1) From 4df16b4258c41d3b6cfb0d9f7e4b9a51f9cba0a0 Mon Sep 17 00:00:00 2001 From: ironartisan Date: Thu, 19 Aug 2021 15:05:17 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A00279.=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E5=B9=B3=E6=96=B9=E6=95=B0java=E7=89=88=E6=9C=AC=E4=BA=8C?= =?UTF-8?q?=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0279.完全平方数.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0279.完全平方数.md b/problems/0279.完全平方数.md index f23453da..3c0f0414 100644 --- a/problems/0279.完全平方数.md +++ b/problems/0279.完全平方数.md @@ -226,7 +226,7 @@ class Solution: return dp[n] def numSquares1(self, n: int) -> int: - '''版本二''' + '''版本二, 先遍历物品, 再遍历背包''' # 初始化 nums = [i**2 for i in range(1, n + 1) if i**2 <= n] dp = [10**4]*(n + 1)