Skip to content

Commit 3211e07

Browse files
author
邵联飞
committed
第六周做野
1 parent 94aa964 commit 3211e07

3 files changed

Lines changed: 120 additions & 1 deletion

File tree

Week06/NOTE.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,48 @@
1-
学习笔记
1+
学习笔记
2+
3+
本周学习动态规划
4+
复习递归的伪代码
5+
java
6+
public void recur(int level, int param) {
7+
// terminator 递归的结束条件
8+
if (level > MAX_LEVEL) {
9+
// process result
10+
return;
11+
}
12+
// process current logic 处理当前节点
13+
process(level, param);
14+
// drill down 进入下一节点
15+
recur( level: level + 1, newParam);
16+
// restore current status 处理返回
17+
18+
}
19+
20+
分治的伪代码
21+
C/C++
22+
int divide_conquer(Problem *problem, int params) {
23+
// recursion terminator
24+
if (problem == nullptr) {
25+
process_result
26+
return return_result;
27+
}
28+
29+
// process current problem
30+
subproblems = split_problem(problem, data)
31+
subresult1 = divide_conquer(subproblem[0], p1)
32+
subresult2 = divide_conquer(subproblem[1], p1)
33+
subresult3 = divide_conquer(subproblem[2], p1)
34+
...
35+
36+
// merge
37+
result = process_result(subresult1, subresult2, subresult3)
38+
// revert the current level status
39+
40+
return 0;
41+
}
42+
43+
44+
定义:讲一个复杂的问题,将它分解为简单的子问题(用递归的方式)
45+
关键点:
46+
1.动态规划与递归或者分治没有本质上的区别(关键看有无最优的子结构)
47+
2.共性:找到重复子问题
48+
3.差异性:最优子结构,中途可淘汰次优解

Week06/first.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
//题目:回文子串
3+
//动态规划法
4+
class Solution {
5+
public int countSubstrings(String s) {
6+
if (s == null || s.equals("")) {
7+
return 0;
8+
}
9+
int n = s.length();
10+
boolean[][] dp = new boolean[n][n];
11+
int result = s.length();
12+
for (int i = 0; i < n; i++) dp[i][i] = true;
13+
for (int i = n - 1; i >= 0; i--) {
14+
for (int j = i + 1; j < n; j++) {
15+
if (s.charAt(i) == s.charAt(j)) {
16+
if (j - i == 1) {
17+
dp[i][j] = true;
18+
} else {
19+
dp[i][j] = dp[i + 1][j - 1];
20+
}
21+
} else {
22+
dp[i][j] = false;
23+
}
24+
if (dp[i][j]) {
25+
result++;
26+
}
27+
}
28+
}
29+
return result;
30+
31+
}
32+
}
33+
34+

Week06/second.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
//题目: 最大正方形
3+
4+
5+
struct Node{
6+
int row_nums;
7+
int col_nums;
8+
Node():row_nums(0), col_nums(0){}
9+
};
10+
11+
class Solution {
12+
public:
13+
int maximalSquare(vector<vector<char>>& matrix) {
14+
if(matrix.empty() || matrix[0].empty()) return 0;
15+
int rows = matrix.size(), cols = matrix[0].size();
16+
// 1. 确定dp数组和状态
17+
vector<vector<Node>> dp(rows+1, vector<Node>(cols+1));
18+
// 2. base case(dp[0][...]=0 & dp[...][0]=0)
19+
// 3. 状态转移方程
20+
for(int i = 1; i <= rows; i++){
21+
for(int j = 1; j <= cols; j++){
22+
if(matrix[i-1][j-1] == '1'){
23+
dp[i][j].row_nums = min(dp[i-1][j].row_nums, dp[i-1][j-1].row_nums)+1;
24+
dp[i][j].col_nums = min(dp[i][j-1].col_nums, dp[i-1][j-1].col_nums)+1;
25+
}
26+
}
27+
}
28+
// 4. 遍历dp计算最大正方形
29+
int max_size = 0;
30+
for(int i = 1; i <= rows; i++){
31+
for(int j = 1; j <= cols; j++){
32+
int edge = min(dp[i][j].row_nums, dp[i][j].col_nums);
33+
max_size = max(max_size, edge*edge);
34+
}
35+
}
36+
return max_size;
37+
}
38+
};

0 commit comments

Comments
 (0)