动态规划阶段总结

1、Maximum Subarray,最大连续子序列

class Solution {

public:

int maxSubArray(vector& nums) {

int n=nums.size();

int dp[n]={0};

dp[0]=nums[0];

int maxum=nums[0];

for(int i=1;i

dp[i]=max(dp[i-1]+nums[i],nums[i]);

maxum=max(maxum,dp[i]);

}

return maxum;

}

};

 

2、Minimum Path Sum

Medium

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.

Note: You can only move either down or right at any point in time.

Example:

Input: [   [1,3,1], [1,5,1], [4,2,1] ] Output: 7 Explanation: Because the path 1→3→1→1→1 minimizes the sum.

class Solution {

public:

int minPathSum(vector>& grid) {

int cow=grid[0].size();

int row=grid.size();

if(row==0) return 0;

int dp[row+1][cow+1];

memset(dp,0,sizeof(dp));

dp[0][0]=grid[0][0];

for(int i=1;i

dp[i][0]=grid[i][0]+dp[i-1][0];

}

for(int i=1;i

dp[0][i]=grid[0][i]+dp[0][i-1];

}

for(int i=1;i

for(int j=1;j

dp[i][j]=min(dp[i-1][j],dp[i][j-1])+grid[i][j];

}

}

return dp[row-1][cow-1];

}

};

3、70. Climbing Stairs

class Solution {

public:

int climbStairs(int n) {

vector nums(n+1,0);

return Mesh(n,nums);

}

int Mesh(int n,vector &mesh){

if(n<3)

return mesh[n]=n;

else if(mesh[n]==0)

mesh[n]=Mesh(n-1,mesh)+Mesh(n-2,mesh);

return mesh[n];

}

};

4、Triangle

class Solution {

public:

int minimumTotal(vector>& triangle) {

int n=triangle.size();

int m=triangle[0].size();

// dp[n][m]={0};

vector res(triangle[n-1]);

for(int i =n-2;i>=0;i--){

for(int j=0;j

res[j]=triangle[i][j]+min(res[j],res[j+1]);

}

}

return res[0];

}

};

 

5、股票买卖,只能买卖一次:以下代码没过,不知为何

class Solution {

public:

int maxProfit(vector& prices) {

int n =prices.size();

int dp[n][2]={0}; //0买,1卖

dp[0][0]==prices[0];

dp[0][1]=0;

for(int i=1;i

dp[i][0]=min(prices[i],dp[i-1][0]);

dp[i][1]=max(dp[i-1][1],prices[i]-dp[i-1][0]);

}

return dp[n-1][1];

}

};

 

6、House Robber ,隔屋抢劫

class Solution {

public:

int rob(vector& nums) {

int n =nums.size();

if(n==0) return 0;

if(n==1) return nums[0];

else if(n==2) return nums[1]>nums[0]?nums[1]:nums[0];

else {

vector dp(n+1,0);

dp[0]=nums[0];

dp[1]=max(nums[0],nums[1]);

for(int i =2;i

dp[i]=max(dp[i-1],dp[i-2]+nums[i]);

}

return dp[n-1];

}

}

};

 

7、Coin Change 多少个零钱可以兑换

class Solution {

public:

int coinChange(vector& coins, int amount) {

sort(coins.begin(),coins.end());

int n =coins.size();

vector res(amount+1,INT_MAX);

res[0]=0;

for(int i=1;i<=amount;i++){

for(auto coin:coins){

if(coin<=i&&res[i-coin]!=INT_MAX){

res[i]=min(res[i],res[i-coin]+1);

}

}

}

return res[amount]==INT_MAX?-1:res[amount];

}

};

 

8、Combination sum4:

nums = [1, 2, 3] target = 4 The possible combination ways are: (1, 1, 1, 1) (1, 1, 2) (1, 2, 1) (1, 3) (2, 1, 1) (2, 2) (3, 1) Note that different sequences are counted as different combinations. Therefore the output is 7.

 

class Solution {

public:

int combinationSum4(vector& nums, int target) {

vector result(target + 1);

result[0] = 1;

for (int i = 1; i <= target; ++i) {

for (int x : nums) {

if (i >= x) {

result[i] += result[i - x];

}

}

}

return result[target];

}

};

9.Delect and Earn

* In each operation, you pick any nums[i] and delete it to earn nums[i]

* points. After, you must delete every element equal to nums[i] - 1 or nums[i]

* + 1.

 

 

class Solution {

public:

int deleteAndEarn(vector& nums) {

if(nums.size()==0) return 0;

sort(nums.begin(),nums.end());

int n=nums[nums.size()-1];

vector dp(n+1,0);

for(auto num:nums){

dp[num]+=num;

}

for(int i =3;i<=n;i++){

dp[i]=max(dp[i-2],dp[i-3])+dp[i];

}

return max(dp[n],dp[n-1]);

}

};

 

 

 

10、最长公共子序列:(连续)

class Solution {

public:

int findLength(vector& A, vector& B) {

int result=0;

vector> dp(A.size(),vector(B.size(),0));

// dp[0][0]=A[0]==B[0]?1:0;

for(int i=0;i

for(int j=0;j

if(A[i]!=B[j]) dp[i][j]=0;

else{

if(i==0||j==0){

dp[i][j]=1;

}else

dp[i][j]=dp[i-1][j-1]+1;

}

if(dp[i][j]>result)

result=dp[i][j];

}

}

return result;

}

};

 

11、最长公共序列:(不连续)

for (int i = 0; i <= x.size(); i++) dp[i][0] = 0; for (int i = 0; i <= y.size(); i++) dp[0][i] = 0; for (int i = 1; i <= x.size(); i++) { for (int j = 1; j <= y.size(); j++) { if (x[i] == y[j]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = dp[i - 1][j] > dp[i][j - 1] ? dp[i - 1][j] : dp[i][j - 1]; } } }

 

12、最长回文串

 

 

class Solution {

public:

string longestPalindrome(string s) {

const int n=s.size();

bool dp[n][n];

fill_n(&dp[0][0],n*n,false);

int max_len=1; //保存最长回文子串长度

int start=0;//保存最长回文子串起点

for(int i=0;i

{

for(int j=0;j<=i;++j)

{

if(i-j<2)

dp[j][i]=(s[i]==s[j]);

else

dp[j][i]=(s[i]==s[j] && dp[j+1][i-1]);

if(dp[j][i] && max_len<(i-j+1))

{

max_len=i-j+1;

start=j;

}

}

}

return s.substr(start,max_len);

}

}

 

13.买卖股票含手续费

int main() {

// int F, N;

// cin >> F >> N;

// //int dp[NF][2];

// vector res(N + 1, 0);

// for (int i = 0; i < N; i++) {

// cin >> res[i];

// }

// dp[0][0] = -res[0];

// dp[0][1] = 0;

// for (int i = 1; i < N; i++) {

// dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - res[i]);

// dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + res[i]-F);

// }

// cout << dp[N - 1][1] << endl;

// return 0;

//}

01背包

 

int knapsack01< const vector &w,const vector &v,int c)

{

if(!(w.size()==v.size()&&c>=0))

int n=w.size();

if(n==0||c==0)

return 0;

vector memo(c+1,0);

for(int i=0;i

for(int j=c;j>=w[i];j--)

memo[j]=max(memo[j],v[i]+memo[j-w[i]]);

return memo[c];

}

 

 

完全背包

 

int knapsack01< const vector &w,const vector &v,int c)

{

if(!(w.size()==v.size()&&c>=0))

int n=w.size();

if(n==0||c==0)

return 0;

vector memo(c+1,0);

for(int i=0;i

for(int j=w[i];j=

memo[j]=max(memo[j],v[i]+memo[j-w[i]]);

return memo[c];

}

 

 

 

你可能感兴趣的:(算法)