添加动态规划题目
This commit is contained in:
parent
0d75cae7f2
commit
2657d9aa86
68
dp/322.cpp
Normal file
68
dp/322.cpp
Normal 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
53
fbl.cpp
Normal 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;
|
||||
}
|
53
tree/Binary_tree/99/main.cpp
Normal file
53
tree/Binary_tree/99/main.cpp
Normal 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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user