添加动态规划题目

This commit is contained in:
zhuyijun 2021-11-11 00:03:12 +08:00
parent 0d75cae7f2
commit 2657d9aa86
5 changed files with 174 additions and 0 deletions

68
dp/322.cpp Normal file
View File

@ -0,0 +1,68 @@
/*
* @Description:
* @Version: 1.0
* @Autor: zhuyijun
* @Date: 2021-11-10 22:49:50
* @LastEditTime: 2021-11-11 00:01:43
*/
/*
coins amount
 -1
 1
coins = [1, 2, 5], amount = 11
3
11 = 5 + 5 + 1
*/
#include <bits/stdc++.h>
using namespace std;
int helper(vector<int>& coins, int amount, vector<int>& list) {
if (amount == 0) return 0;
if (amount < 0) return -1;
if (list[amount] != -1) return list[amount];
int res = INT_MAX;
for (int i = 0; i < coins.size(); i++) {
int temp = helper(coins, amount - coins[i], list);
if (temp == -1) continue;
res = min(res, temp + 1);
}
if (res == INT_MAX) {
res = -1;
}
list[amount] = res;
return list[amount];
}
// 暴力递归解法 超时
int coinChange1(vector<int>& coins, int amount) {
vector<int> list(amount + 1, -1);
return helper(coins, amount, list);
}
// dp 数组迭代解法
int coinChange(vector<int>& coins, int amount) {
vector<int> list(amount + 1, amount + 1);
list[0] = 0;
for (int i = 0; i < list.size(); i++) {
for (int coin : coins) {
if (i - coin < 0) {
continue;
}
list[i] = min(list[i], 1 + list[i - coin]);
}
}
return (list[amount] == amount + 1) ? -1 : list[amount];
}
int main() {
vector<int> list{186, 419, 83, 408};
cout << coinChange(list, 6249);
}

53
fbl.cpp Normal file
View File

@ -0,0 +1,53 @@
/*
* @Description:
* @Version: 1.0
* @Autor: zhuyijun
* @Date: 2021-11-10 21:56:48
* @LastEditTime: 2021-11-10 22:22:02
*/
#include <bits/stdc++.h>
using namespace std;
int helper(vector<int> list, int n) {
if (n == 1 || n == 2) {
return 1;
}
//判断该n是否已经计算过如果不等于0则已经计算过 获取其值
if (list[n] != 0) return list[n];
list[n] = helper(list, n - 1) + helper(list, n - 2);
return list[n];
}
int fbl(int n) {
//维护一个备忘录 倍计算过的值都会被记录到里面
vector<int> list(n + 1, 0);
return helper(list, n);
}
// dp方法 1
int fbl_dp(int n) {
vector<int> dp(n + 1, 0);
dp[1] = dp[2] = 1;
for (int i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
// dp 优化
int fbl_dp2(int n) {
if (n == 1 || n == 2) {
return 1;
}
int prev = 1, curr = 1;
for (int i = 3; i <= n; i++) {
int sum = prev + curr;
prev = curr;
curr = sum;
}
return curr;
}
int main() {
std::cout << fbl(20) << std::endl;
std::cout << fbl_dp(20) << std::endl;
std::cout << fbl_dp2(20) << std::endl;
return 0;
}

View File

@ -0,0 +1,53 @@
/*
* @Description:
* @Version: 1.0
* @Autor: zhuyijun
* @Date: 2021-11-10 20:51:48
* @LastEditTime: 2021-11-10 21:43:10
*/
//恢复二叉搜索树
/*
root
使 O(n)
使
root = [1,3,null,null,2]
[3,1,null,null,2]
3 1 3 > 1 1 3 使
*/
/*
* BST , S[a] < S[a+1]
* S[a] > S[a+1] S[a] , S[a] > S[a+1] S[a+1]
*/
#include <bits/stdc++.h>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right)
: val(x), left(left), right(right) {}
};
TreeNode *t1, *t2, *pre;
void recoverTree(TreeNode *root) {
inorder(root);
int temp = t1->val;
t1->val = t2->val;
t2->val = temp;
}
void inorder(TreeNode *root) {
if (root == nullptr) return;
inorder(root->left);
if (pre != nullptr && pre->val > root->val) {
if (t1 == nullptr) t1 = pre;
t2 = root;
}
pre = root;
inorder(root->right);
}