Skip to content

Commit b097575

Browse files
committed
95 312
1 parent b270797 commit b097575

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Leetcode刷题记录
1515
+ No.126 [cpp](cpp/126.cpp) [python](python/126.py)
1616
+ No.543 [cpp](cpp/543.cpp) [python](python/543.py)
1717
+ No.785 [cpp-dfs](cpp/785.cpp) [cpp-bfs](cpp/785-bfs.cpp) **二分图染色问题**
18+
+ No.95 [cpp](cpp/95.cpp)
1819

1920
### 并查集
2021

@@ -72,6 +73,7 @@ Leetcode刷题记录
7273
+ No.126 [cpp](cpp/126.cpp) [python](python/126.py)
7374
+ No.1014 [cpp](cpp/1014.cpp) [python](python/1014.py) easy
7475
+ No.174 [cpp](cpp/174.cpp) [python](python/174.py) [java](java/Problem174.java)
76+
+ No.312 [cpp](cpp/312.cpp)
7577

7678
### 状态机DP
7779

cpp/312.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class Solution {
5+
public:
6+
int maxCoins(vector<int>& nums) {
7+
int n = nums.size();
8+
vector<int> vec(n+2, 1); // 让开最后一个和第一个放1
9+
for (int i = 1; i <= n; ++i) vec[i] = nums[i-1];
10+
11+
vector<vector<int>> dp(n+2, vector<int>(n+2, 0));
12+
for (int i = n+1; i >= 0; --i) {
13+
for (int j = i; j <= n+1; ++j) {
14+
if (j - i < 2) dp[i][j] = 0;
15+
for (int k = i+1; k <= j-1; ++k) {
16+
dp[i][j] = max(dp[i][j], dp[i][k] + dp[k][j] + vec[i] * vec[k] * vec[j]);
17+
}
18+
}
19+
}
20+
return dp[0][n+1];
21+
}
22+
};

cpp/95.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
/**
4+
* Definition for a binary tree node.
5+
* struct TreeNode {
6+
* int val;
7+
* TreeNode *left;
8+
* TreeNode *right;
9+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
10+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
11+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
12+
* };
13+
*/
14+
15+
struct TreeNode {
16+
int val;
17+
TreeNode *left;
18+
TreeNode *right;
19+
TreeNode() : val(0), left(nullptr), right(nullptr) {}
20+
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
21+
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
22+
};
23+
24+
25+
class Solution {
26+
public:
27+
vector<TreeNode*> generateTrees(int n) {
28+
if(n == 0) return {};
29+
return generateTrees(1,n);
30+
}
31+
32+
vector<TreeNode*> generateTrees(int begin, int end) {
33+
34+
vector<TreeNode*> ans;
35+
if(begin > end) return {nullptr}; // 返回, 边界条件
36+
// 递归代码
37+
for(int i=begin; i<=end; i++) { // 将任意作为中心
38+
for(auto& l : generateTrees(begin, i-1)) { // 前半部分
39+
for(auto& r : generateTrees(i+1, end)) { // 后半部分
40+
TreeNode* root = new TreeNode(i,l,r);
41+
ans.push_back(root);
42+
}
43+
}
44+
}
45+
return ans;
46+
}
47+
};

0 commit comments

Comments
 (0)