Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 73c63cc

Browse files
Merge pull request youngyangyang04#624 from ironartisan/master
添加0279.完全平方数python3版本一解法
2 parents 391a04b + 4ba8948 commit 73c63cc

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

‎problems/0084.柱状图中最大的矩形.md‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,40 @@ public:
195195

196196
Java:
197197

198+
动态规划
199+
```java
200+
class Solution {
201+
public int largestRectangleArea(int[] heights) {
202+
int length = heights.length;
203+
int[] minLeftIndex = new int [length];
204+
int[] maxRigthIndex = new int [length];
205+
206+
// 记录左边第一个小于该柱子的下标
207+
minLeftIndex[0] = -1 ;
208+
for (int i = 1; i < length; i++) {
209+
int t = i - 1;
210+
// 这里不是用if,而是不断向右寻找的过程
211+
while (t >= 0 && heights[t] >= heights[i]) t = minLeftIndex[t];
212+
minLeftIndex[i] = t;
213+
}
214+
// 记录每个柱子 右边第一个小于该柱子的下标
215+
maxRigthIndex[length - 1] = length;
216+
for (int i = length - 2; i >= 0; i--) {
217+
int t = i + 1;
218+
while(t < length && heights[t] >= heights[i]) t = maxRigthIndex[t];
219+
maxRigthIndex[i] = t;
220+
}
221+
// 求和
222+
int result = 0;
223+
for (int i = 0; i < length; i++) {
224+
int sum = heights[i] * (maxRigthIndex[i] - minLeftIndex[i] - 1);
225+
result = Math.max(sum, result);
226+
}
227+
return result;
228+
}
229+
}
230+
```
231+
198232
Python:
199233

200234
动态规划

‎problems/0279.完全平方数.md‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ public:
161161
Java:
162162
```Java
163163
class Solution {
164+
// 版本一,先遍历物品, 再遍历背包
164165
public int numSquares(int n) {
165166
int max = Integer.MAX_VALUE;
166167
int[] dp = new int[n + 1];
@@ -170,7 +171,9 @@ class Solution {
170171
}
171172
//当和为0时,组合的个数为0
172173
dp[0] = 0;
174+
// 遍历物品
173175
for (int i = 1; i * i <= n; i++) {
176+
// 遍历背包
174177
for (int j = i * i; j <= n; j++) {
175178
if (dp[j - i * i] != max) {
176179
dp[j] = Math.min(dp[j], dp[j - i * i] + 1);
@@ -180,14 +183,36 @@ class Solution {
180183
return dp[n];
181184
}
182185
}
186+
187+
class Solution {
188+
// 版本二, 先遍历背包, 再遍历物品
189+
public int numSquares(int n) {
190+
int max = Integer.MAX_VALUE;
191+
int[] dp = new int[n + 1];
192+
// 初始化
193+
for (int j = 0; j <= n; j++) {
194+
dp[j] = max;
195+
}
196+
// 当和为0时,组合的个数为0
197+
dp[0] = 0;
198+
// 遍历背包
199+
for (int j = 1; j <= n; j++) {
200+
// 遍历物品
201+
for (int i = 1; i * i <= j; i++) {
202+
dp[j] = Math.min(dp[j], dp[j - i * i] + 1);
203+
}
204+
}
205+
return dp[n];
206+
}
207+
}
183208
```
184209

185210
Python:
186211

187212
```python3
188213
class Solution:
189214
def numSquares(self, n: int) -> int:
190-
'''版本一'''
215+
'''版本一,先遍历背包, 再遍历物品'''
191216
# 初始化
192217
nums = [i**2 for i in range(1, n + 1) if i**2 <= n]
193218
dp = [10**4]*(n + 1)
@@ -201,7 +226,7 @@ class Solution:
201226
return dp[n]
202227

203228
def numSquares1(self, n: int) -> int:
204-
'''版本二'''
229+
'''版本二, 先遍历物品, 再遍历背包'''
205230
# 初始化
206231
nums = [i**2 for i in range(1, n + 1) if i**2 <= n]
207232
dp = [10**4]*(n + 1)
@@ -217,6 +242,22 @@ class Solution:
217242
Python3:
218243
```python
219244
class Solution:
245+
'''版本一,先遍历背包, 再遍历物品'''
246+
def numSquares(self, n: int) -> int:
247+
dp = [n] * (n + 1)
248+
dp[0] = 0
249+
# 遍历背包
250+
for j in range(1, n+1):
251+
for i in range(1, n):
252+
num = i ** 2
253+
if num > j: break
254+
# 遍历物品
255+
if j - num >= 0:
256+
dp[j] = min(dp[j], dp[j - num] + 1)
257+
return dp[n]
258+
259+
class Solution:
260+
'''版本二, 先遍历物品, 再遍历背包'''
220261
def numSquares(self, n: int) -> int:
221262
# 初始化
222263
# 组成和的完全平方数的最多个数,就是只用1构成

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /