-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
liujj
committed
Aug 8, 2022
1 parent
9fc1ea8
commit 03a1b77
Showing
9 changed files
with
630 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <cassert> | ||
using namespace std; | ||
|
||
// 有一个背包,他的容量为C(Capacity)。现在又n种不同的物品,编号为0....n - 1 ,其中每一件物品的重量为w(i),价值为v(i).问可以向这个背包中盛放哪些物品,使得在不超过背包容量的基础上,物品的总价值最大 | ||
// 暴力解法:每一件物品都可以放进背包,也可以不放进去 | ||
// 动态规划: | ||
// 状态定义: | ||
// F(n,C) :将n个物品放进容量为C的背包,使得价值最大 | ||
// F(i, c) = F(i - 1, c) // i不放入背包 | ||
// = v(i) + F(i - 1, c- W(i)) // i放入背包 | ||
class Solution | ||
{ | ||
private: | ||
// 用[0 ... index]的物品,填充容积为C的背包的最大价值 | ||
int bestValue(const vector<int> &w, const vector<int> v, int index, int c) | ||
{ | ||
if (index < 0 || c <= 0) | ||
{ | ||
return 0; | ||
} | ||
|
||
int res = bestValue(w, v, index - 1, c); | ||
if (c >= w[index]) | ||
{ | ||
res = max(res, v[index] + bestValue(w, v, index - 1, c - w[index])); | ||
} | ||
|
||
return res; | ||
} | ||
|
||
// 动态规划 + 记忆化搜索 | ||
vector<vector<int>> memo; // | ||
int bestValue2(const vector<int> &w, const vector<int> v, int index, int c) | ||
{ | ||
if (index < 0 || c <= 0) | ||
{ | ||
return 0; | ||
} | ||
|
||
if (memo[index][c] != -1) | ||
{ | ||
return memo[index][c]; | ||
} | ||
|
||
int res = bestValue(w, v, index - 1, c); | ||
if (c >= w[index]) | ||
{ | ||
res = max(res, v[index] + bestValue(w, v, index - 1, c - w[index])); | ||
} | ||
|
||
memo[index][c] = res; | ||
|
||
return res; | ||
} | ||
public: | ||
int knapspack01(const vector<int> &w, const vector<int> &v, int c) | ||
{ | ||
int n = w.size(); | ||
return bestValue(w, v, n - 1, c); | ||
} | ||
int knapspack02(const vector<int> &w, const vector<int> &v, int c) | ||
{ | ||
int n = w.size(); | ||
|
||
memo = vector<vector<int>> (n, vector<int>(c + 1, -1)); // n * C + 1 | ||
return bestValue2(w, v, n - 1, c); | ||
} | ||
int knapspack03(const vector<int> &w, const vector<int> &v, int c) | ||
{ | ||
int n = w.size(); | ||
if (0 == n) | ||
{ | ||
return 0; | ||
} | ||
vector<vector<int>> memo(n, vector<int>(c + 1, -1)); | ||
|
||
// 先考虑0号物品 | ||
for (int j = 0; j <= c; ++j) | ||
{ | ||
memo[0][j] = ( j >= w[0] ? v[0] : 0); | ||
} | ||
|
||
for (int i = 1; i < n; ++i) | ||
{ | ||
for (int j = 0; j <= c; ++j) | ||
{ | ||
memo[i][j] = memo[i - 1][j]; | ||
if (j >= w[i]) | ||
{ | ||
memo[i][j] = max( memo[i][j], v[i] + memo[i - 1][j - w[i]]); | ||
} | ||
} | ||
} | ||
|
||
return memo[n - 1][c]; | ||
} | ||
|
||
int knapspack04(const vector<int> &w, const vector<int> &v, int c) | ||
{ | ||
int n = w.size(); | ||
if (0 == n) | ||
{ | ||
return 0; | ||
} | ||
vector<vector<int>> memo(2, vector<int>(c + 1, -1)); | ||
|
||
// 先考虑0号物品 | ||
for (int j = 0; j <= c; ++j) | ||
{ | ||
memo[0][j] = ( j >= w[0] ? v[0] : 0); | ||
} | ||
|
||
for (int i = 1; i < n; ++i) | ||
{ | ||
for (int j = 0; j <= c; ++j) | ||
{ | ||
memo[i % 2][j] = memo[(i - 1) % 2][j]; | ||
if (j >= w[i]) | ||
{ | ||
memo[i % 2][j] = max( memo[i % 2][j], v[i] + memo[(i - 1) % 2][j - w[i]]); | ||
} | ||
} | ||
} | ||
|
||
return memo[(n - 1) % 2][c]; | ||
} | ||
|
||
}; | ||
|
||
|
||
int main() { | ||
|
||
vector<int> w = {1, 2, 3}; | ||
vector<int> v = {6, 10, 12}; | ||
int c = 5; | ||
|
||
Solution sol; | ||
cout << sol.knapspack01(w, v, c) << endl; | ||
cout << sol.knapspack02(w, v, c) << endl; | ||
cout << sol.knapspack03(w, v, c) << endl; | ||
cout << sol.knapspack04(w, v, c) << endl; | ||
|
||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <iostream> | ||
#include <vector> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
int min(vector<vector<int>> &triangle) | ||
{ | ||
size_t size = triangle.size(); | ||
for (int i = triangle.size(); i > 0; --i) | ||
{ | ||
for (int j = 0; j < triangle[size].size(); ++j) | ||
{ | ||
triangle[size][j] = min(triangle[i][j - 1], triangle[i][j]); | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
int minimumTotal(vector<vector<int>>& triangle) { | ||
|
||
} | ||
}; | ||
|
||
int main() | ||
{ | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include <iostream> | ||
#include <vector> | ||
using namespace std; | ||
|
||
int integerBreak(int n) { | ||
vector<int> dp(n + 1, 0); | ||
dp[1] = 1; | ||
|
||
for (int i = 0; i < n + 1; ++i) | ||
{ | ||
for (int j = i - 1; j >= 1; --j) | ||
{ | ||
dp[i] = max(dp[i], dp[j] * (i - j)); | ||
cout << "max1:" << dp[i] << ", " << dp[j] * (i - j) << endl; | ||
dp[i] = max(dp[i], j * (i - j)); | ||
cout << "max2:" << dp[i] << ", " << j * (i - j) << endl; | ||
} | ||
cout << endl; | ||
} | ||
|
||
return dp[n]; | ||
} | ||
|
||
/** | ||
* 1. 确定dp数组下标含义 分拆数字i,可以得到的最大乘积为dp[i]; | ||
* 2. 递推公式 dp[i] = max(dp[i], (i - j) * j, dp[i - j] * j); | ||
* 3. 初始化 dp[2] = 1; | ||
* 4. 遍历顺序 从前向后遍历就可以; | ||
* 5. 推导结果; | ||
*/ | ||
int integerBreak2(int n) | ||
{ | ||
/* 定义dp数组 */ | ||
vector<int> dp(n + 1, 0); | ||
/* dp数组初始化 */ | ||
dp[2] = 1; | ||
/* 从前向后遍历 */ | ||
cout << "dp[i] " << ",dp[i - j] * j" << ",((i - j) * j)" << endl; | ||
for (int i = 3; i <= n ; i++) | ||
{ | ||
/* j遍历到小于i的值 */ | ||
for (int j = 1; j < i - 1; j++) | ||
{ | ||
cout << dp[i] << " "; | ||
dp[i] = max(dp[i], dp[i - j] * j); | ||
cout << dp[i - j] * j << " "; | ||
dp[i] = max(dp[i], ((i - j) * j)); | ||
cout << ((i - j) * j) << " | "; | ||
} | ||
cout << "dp[" << i << "]->" << dp[i] << endl; | ||
} | ||
return dp[n]; | ||
} | ||
|
||
int integerBreak3(int n) | ||
{ | ||
if (2 == n) | ||
{ | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
int main() { | ||
|
||
cout << integerBreak2(10) << endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#include <iostream> | ||
#include <cmath> | ||
#include <cstdio> | ||
#include <cstdlib> | ||
#include <vector> | ||
#include <stack> | ||
#include <queue> | ||
#include <string> | ||
#include <map> | ||
#include <set> | ||
#include <algorithm> | ||
using namespace std; | ||
|
||
vector<vector<bool>> col = vector<vector<bool>>(9, vector<bool>(9, false)); | ||
|
||
void print(vector<vector<char>>& v)//, vector<vector<bool>> &col) | ||
{ | ||
for_each(v.begin(), v.end(), | ||
[&](vector<char> &w){ | ||
for_each(w.begin(), w.end(), [&](char &val){ cout << val << ' '; }); | ||
cout << endl; | ||
}); | ||
|
||
// for_each(col.begin(), col.end(), | ||
// [&](vector<bool> &w){ | ||
// for_each(w.begin(), w.end(), [&](bool val){ cout << val << ' '; }); | ||
// cout << endl; | ||
// }); | ||
} | ||
|
||
int c = 0; | ||
|
||
vector<vector<char>> ans; | ||
|
||
void dfs(int x, int y, vector<char> &v, vector<char> &row) | ||
{ | ||
if (y == v.size()) | ||
{ | ||
ans.push_back(row); | ||
return ; | ||
} | ||
|
||
row.push_back(v[y]); | ||
if ('.' == row[y]) | ||
{ | ||
char tmp; | ||
for (int j = 0; j < col[x].size(); ++j) | ||
{ | ||
if (!col[x][j]) | ||
{ | ||
tmp = j + '0'; | ||
} | ||
} | ||
col[x][tmp - '0'] = true; | ||
row[y] = tmp; | ||
} | ||
dfs(x, y + 1, v, row); | ||
|
||
} | ||
|
||
void solveSudoku(vector<vector<char>>& v) | ||
{ | ||
for_each(v.begin(), v.end(), | ||
[&](vector<char> &w){ | ||
for_each(w.begin(), w.end(), | ||
[&](char &val){ | ||
if ('.' != val) | ||
{ | ||
col[c][val - '0'] = true; | ||
} | ||
}); | ||
++c; | ||
}); | ||
|
||
|
||
print(v);//, col); | ||
} | ||
|
||
int main() | ||
{ | ||
vector<vector<char>> v = { | ||
{'5','3','.','.','7','.','.','.','.'},{'6','.','.','1','9','5','.','.','.'},{'.','9','8','.','.','.','.','6','.'}, | ||
{'8','.','.','.','6','.','.','.','3'},{'4','.','.','8','.','3','.','.','1'},{'7','.','.','.','2','.','.','.','6'}, | ||
{'.','6','.','.','.','.','2','8','.'},{'.','.','.','4','1','9','.','.','5'},{'.','.','.','.','8','.','.','7','9'} | ||
}; | ||
|
||
solveSudoku(v); | ||
|
||
// char c = 2 + '0'; | ||
// cout << c << endl; | ||
vector<char> v1 = {'5','3','.','.','7','.','.','.','.'}; | ||
vector<char> row; | ||
dfs(0, 0, v1, row); | ||
cout << "----" << endl; | ||
print(ans); | ||
// for_each(v1.begin(), v1.end(), [](char &v) { cout << v << " ";}); | ||
|
||
|
||
return 0; | ||
} |
Oops, something went wrong.