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 eb92cb0

Browse files
Merge pull request youngyangyang04#2154 from jianghongcheng/master
merge
2 parents 0829e1f + 7198030 commit eb92cb0

File tree

5 files changed

+92
-1
lines changed

5 files changed

+92
-1
lines changed

‎problems/0063.不同路径II.md‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,39 @@ class Solution:
397397

398398
```
399399

400+
动态规划(版本五)
400401

402+
```python
403+
class Solution:
404+
def uniquePathsWithObstacles(self, obstacleGrid):
405+
if obstacleGrid[0][0] == 1:
406+
return 0
407+
408+
m, n = len(obstacleGrid), len(obstacleGrid[0])
409+
410+
dp = [0] * n # 创建一个一维列表用于存储路径数
411+
412+
# 初始化第一行的路径数
413+
for j in range(n):
414+
if obstacleGrid[0][j] == 1:
415+
break
416+
dp[j] = 1
417+
418+
# 计算其他行的路径数
419+
for i in range(1, m):
420+
if obstacleGrid[i][0] == 1:
421+
dp[0] = 0
422+
for j in range(1, n):
423+
if obstacleGrid[i][j] == 1:
424+
dp[j] = 0
425+
continue
426+
427+
dp[j] += dp[j - 1]
428+
429+
return dp[-1] # 返回最后一个元素,即终点的路径数
430+
431+
432+
```
401433
### Go
402434

403435
```go

‎problems/0343.整数拆分.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,11 @@ class Solution:
262262

263263
# 计算切割点j和剩余部分(i-j)的乘积,并与之前的结果进行比较取较大值
264264

265-
dp[i] = max(dp[i], max((i - j) * j, dp[i - j] * j))
265+
dp[i] = max(dp[i], (i - j) * j, dp[i - j] * j)
266266

267267
return dp[n] # 返回最终的计算结果
268268

269+
269270
```
270271
动态规划(版本二)
271272
```python

‎problems/0416.分割等和子集.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,21 @@ class Solution:
383383
return True
384384
return False
385385

386+
```
387+
388+
卡哥版(简化版)
389+
```python
390+
class Solution:
391+
def canPartition(self, nums: List[int]) -> bool:
392+
if sum(nums) % 2 != 0:
393+
return False
394+
target = sum(nums) // 2
395+
dp = [0] * (target + 1)
396+
for num in nums:
397+
for j in range(target, num-1, -1):
398+
dp[j] = max(dp[j], dp[j-num] + num)
399+
return dp[-1] == target
400+
386401
```
387402
二维DP版
388403
```python

‎problems/0746.使用最小花费爬楼梯.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,35 @@ class Solution:
282282
return dp1 # 返回到达楼顶的最小花费
283283

284284
```
285+
动态规划(版本三)
285286

287+
```python
288+
class Solution:
289+
def minCostClimbingStairs(self, cost: List[int]) -> int:
290+
dp = [0] * len(cost)
291+
dp[0] = cost[0] # 第一步有花费
292+
dp[1] = cost[1]
293+
for i in range(2, len(cost)):
294+
dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]
295+
# 注意最后一步可以理解为不用花费,所以取倒数第一步,第二步的最少值
296+
return min(dp[-1], dp[-2])
297+
298+
```
299+
动态规划(版本四)
300+
301+
```python
302+
class Solution:
303+
def minCostClimbingStairs(self, cost: List[int]) -> int:
304+
n = len(cost)
305+
prev_1 = cost[0] # 前一步的最小花费
306+
prev_2 = cost[1] # 前两步的最小花费
307+
for i in range(2, n):
308+
current = min(prev_1, prev_2) + cost[i] # 当前位置的最小花费
309+
prev_1, prev_2 = prev_2, current # 更新前一步和前两步的最小花费
310+
return min(prev_1, prev_2) # 最后一步可以理解为不用花费,取倒数第一步和第二步的最少值
311+
312+
313+
```
286314
### Go
287315
```Go
288316
func minCostClimbingStairs(cost []int) int {

‎problems/1049.最后一块石头的重量II.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,21 @@ class Solution:
238238

239239
return total_sum - dp[target] - dp[target]
240240

241+
```
242+
243+
卡哥版(简化版)
244+
```python
245+
class Solution:
246+
def lastStoneWeightII(self, stones):
247+
total_sum = sum(stones)
248+
target = total_sum // 2
249+
dp = [0] * (target + 1)
250+
for stone in stones:
251+
for j in range(target, stone - 1, -1):
252+
dp[j] = max(dp[j], dp[j - stone] + stone)
253+
return total_sum - 2* dp[-1]
254+
255+
241256
```
242257
二维DP版
243258
```python

0 commit comments

Comments
(0)

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