class Solution {
public:intfindMaximizedCapital(int k,int W, vector<int>& Profits, vector<int>& Capital){int n = Profits.size();
vector<pair<int,int>>tmp(n);for(int i =0; i < n; i ++)
tmp[i]=make_pair(Capital[i], Profits[i]);//将项目按照资本从小到大排序。sort(tmp.begin(), tmp.end());
priority_queue<int>heap;//存储最大利润的大根堆int i =0;while(k --){while(i < n && W >= tmp[i].first){
heap.push(tmp[i].second);
i++;}if(!heap.empty()){
W += heap.top();
heap.pop();}}return W;}};
class Solution {
public:
vector<int>nextGreaterElements(vector<int>& nums){
vector<int>res(nums.size(),-1);
vector<int>tmp(2* nums.size(),-1);for(int i =0; i < nums.size(); i ++){
tmp[i]= nums[i];
tmp[nums.size()+ i]= nums[i];}for(int i =0; i < nums.size(); i ++)for(int j = i; j < tmp.size(); j ++)if(tmp[j]> nums[i]){
res[i]= tmp[j];break;}return res;}};
class Solution {
public:
vector<int>nextGreaterElements(vector<int>& nums){int n = nums.size();
vector<int>res(n,-1);
stack<int> stk;for(int i =0; i <2* n; i ++){while(!stk.empty()&& nums[i % n]> nums[stk.top()])
res[stk.top()]= nums[i % n], stk.pop();if(i < n) stk.push(i);}return res;}};
class Solution {
public:
string convertToBase7(int num){
string res;if(num ==0)return"0";
bool is_neg = false;if(num <0) is_neg = true, num *=-1;while(num){
res +=to_string(num %7);
num /=7;}if(is_neg) res +='-';reverse(res.begin(), res.end());return res;}};
class Solution {
public:
vector<string>findRelativeRanks(vector<int>& nums){int n = nums.size();
vector<string>res(n);
map<int,int>m;//map自动排序 for(int i =0; i < n; i ++)
m[nums[i]]= i;for(auto x : m){if(n ==1) res[x.second]="Gold Medal";elseif(n ==2) res[x.second]="Silver Medal";elseif(n ==3) res[x.second]="Bronze Medal";else res[x.second]=to_string(n);
n --;}return res;}};
class Solution {
public:
bool checkPerfectNumber(int num){if(num ==1)return false;int sum =0;for(int i =2; i * i < num; i ++)
sum += num % i ==0? i + num / i :0;//2 18 3 12 4 9 return sum +1== num;}};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int>findFrequentTreeSum(TreeNode* root){int max_count =0;
unordered_map<int,int>m;dfs(root, m, max_count);
vector<int>res;for(auto&p : m){if(p.second == max_count)
res.push_back(p.first);}return res;}intdfs(TreeNode* root, unordered_map<int,int>&m,int&max_count){if(!root)return0;int l =dfs(root->left, m, max_count);int r =dfs(root->right, m, max_count);int s = l + r + root->val;
max_count =max(max_count,++m[s]);return s;}};
class Solution {
public:intfib(int N){if(N ==0)return0;if(N ==1)return1;returnfib(N -1)+fib(N -2);}};
class Solution {
public:intfib(int N){
vector<int>dp(N +3,0);// base case
dp[1]= dp[2]=1;//这里有bug 上面的初始化要大一点,不然数组越界for(int i =3; i <= N; i++)
dp[i]= dp[i -1]+ dp[i -2];return dp[N];}};
class Solution {
public:intfib(int N){if(N ==0)return0;if(N ==1|| N ==2)return1;int prev =1, curv =1;for(int i =3; i <= N; i ++){int sum = prev + curv;
prev = curv;
curv = sum;}return curv;}};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:intfindBottomLeftValue(TreeNode* root){if(!root)return-1;
queue<TreeNode*>q;
q.push(root);
TreeNode*p;while(!q.empty()){
p = q.front(), q.pop();if(p->right) q.push(p->right);if(p->left) q.push(p->left);}return p->val;}};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:int res;int max = INT_MIN;intfindBottomLeftValue(TreeNode* root){dfs(root,0);return res;}voiddfs(TreeNode* root,int depth){if(root){if(!root->left &&!root->right)//叶子节点{if(depth > max){
max = depth;
res = root->val;}}dfs(root->left, depth +1);dfs(root->right, depth +1);}}};
class Solution {
public:intfindRotateSteps(string ring, string key){int m = ring.size(), n = key.size(), cost =0, res = INT_MAX;int dp[n][m];memset(dp,0, n * m *sizeof(int));//初始化for(int i =0; i < m; i ++)if(ring[i]== key[0])
dp[0][i]=min(i +1, m - i +1);for(int i =1; i < n; i ++)for(int j =0; j < m; j ++){
dp[i][j]= INT_MAX;if(key[i]== ring[j]){for(int k =0; k < m; k ++)//上一次十二点的位置{if(key[i -1]== ring[k]){
cost = dp[i -1][k]+min((j - k + m)% m,(k - j + m)% m)+1;
dp[i][j]=min(dp[i][j], cost);}}}}for(int i =0; i < m; i ++)if(ring[i]== key[n -1])
res =min(res, dp[n -1][i]);return res;}};