File tree 1 file changed +52
-0
lines changed
1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change @@ -21,6 +21,31 @@ const dpDoubleCode = `
21
21
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
22
22
` ;
23
23
24
+ const dpRangeCode = `
25
+ class Solution:
26
+ def solve(self, s):
27
+ n = len(s)
28
+ dp = [[0] * n for _ in range(n)]
29
+ # 右边界倒序遍历
30
+ for i in range(n - 1, -1, -1):
31
+ # 左边界正序遍历
32
+ for j in range(i + 1, n):
33
+ # do something
34
+ return dp[0][m-1] # 一般都是使用这个区间作为答案
35
+ `
36
+ const dpRangeCodeRecur = `
37
+ class Solution:
38
+ def solve(self, s):
39
+ @lru_cache(None)
40
+ def helper(l, r):
41
+ if l >= r:
42
+ return 0
43
+ if s[l] == s[r]:
44
+ return helper(l + 1, r - 1)
45
+ return 1 + min(helper(l + 1, r), helper(l, r - 1))
46
+ return helper(0, len(s) - 1)
47
+ `
48
+
24
49
const roadmaps = {
25
50
"binary-search" : {
26
51
desc : `
@@ -455,6 +480,33 @@ def count_bs(nums, k):
455
480
code : void 0 ,
456
481
keys : [ ] ,
457
482
} ,
483
+ {
484
+ title : "区间 DP" ,
485
+ // pic:
486
+ // "https://tva1.sinaimg.cn/large/0081Kckwly1glpjptuor0j31n00u0q8y.jpg",
487
+ problems : [
488
+ {
489
+ link : "https://binarysearch.com/problems/Make-a-Palindrome-by-Inserting-Characters" ,
490
+ text : "回文插入" ,
491
+ desc :
492
+ "区间 dp 需要从序列两头同时进行,而不是从序列的某一端到另一端" ,
493
+ } ,
494
+ ] ,
495
+ code : {
496
+ language : "py" ,
497
+ text : `
498
+
499
+ ${ dpRangeCode }
500
+ # 使用记忆化可能会更好书写,比如上面的 dp 代码改成记忆化递归就是:
501
+ ${ dpRangeCodeRecur } `,
502
+ } ,
503
+ keys : [
504
+ `
505
+ 右边界倒序遍历,左边界正序遍历
506
+ ` ,
507
+ "通常都是返回 dp[0][n],而不是其他常见的 dp[-1][-1]" ,
508
+ ] ,
509
+ } ,
458
510
{
459
511
title : "状态压缩型(仅列举题目)" ,
460
512
pic : "" ,
You can’t perform that action at this time.
0 commit comments