6. leetcode(动态规划-基础)

这篇博客介绍了使用动态规划解决一系列经典算法问题,包括LeetCode上的509斐波那契数、70爬楼梯、746使用最小花费爬楼梯、62不同路径和63不同路径II。每个问题都提供了两种或更多种解决方案,包括优化状态压缩以降低空间复杂度。此外,还讨论了343整数拆分和96不同的二叉搜索树问题。这些例子展示了动态规划在解决递归问题时的高效性和灵活性。
摘要由CSDN通过智能技术生成

1. leetcode 509 斐波那契数

//1.leetcode 509 斐波那契数

class Solution {
public:
    int fib(int n) {
        if (n <= 1) {
            return n;
        }

        vector<int> dp(n + 1);
        dp[0] = 0;
        dp[1] = 1;
        for (int i = 2; i <= n; i++) {
            dp[i] = dp[i - 1] + dp[i -2]; 
        }
        return dp[n];
    }
};

//优化 状态压缩
class Solution {
public:
    int fib(int n) {
        if (n < 2) {
            return n;
        }
        int pre = 0, cur = 1;
        for (int i = 2; i <= n; i++) {
            int sum = pre + cur;
            pre = cur;
            cur = sum;
        }
        return cur;
    }
};

2.leetcode 70 爬楼梯

//2.leetcode 70. 爬楼梯
class Solution {
public:
    int climbStairs(int n) {
        if (n  <= 2) {
            return n;
        }

        vector<int> dp(n + 1);
        dp[1] = 1;
        dp[2] = 2;
        for (int i = 3; i <= n; i++) {
            dp[i] = dp[i - 2] + dp[i - 1];
        }
        return dp[n];
    }
};

class Solution {
public:
    int climbStairs(int n) {
        if (n <= 2) {
            return n;
        }

        int pre = 1, cur = 2;
        for (int i = 3; i <= n; i++) {
            int sum = pre + cur;
            pre = cur;
            cur = sum;
        }
        return cur;
    }
};

3.leetcode 746 使用最小花费爬楼梯

//3.leetcode 746 使用最小花费爬楼梯

class Solution {
public:
    int minCostClimbingStairs(vector<int>& cost) {
        int size = cost.size();
        vector<int> minCost(size);
        minCost[0] = 0;
        minCost[1] = min(cost[0], cost[1]);
        for (int i = 2; i < size; i++) {
            minCost[i] = min(minCost[i - 1] + cost[i], minCost[i - 2] + cost[i - 1]);
        }
        return minCost[size - 1];
    }
};

//优化
// 只用两个变量保存状态转移方程中前面的两个记录,并不断更新,就可以递推下去,这样空间复杂度就由O(N)变为O(1)了。
class Solution {
public:
    int minCostClimbingStairs(vector<int>& cost) {
        int minCost0 = 0;
        int minCost1 = min(cost[0], cost[1]);
        int minCost;
        for (int i = 2; i < cost.size(); i++) {
            minCost = min(minCost1 + cost[i], minCost0 + cost[i - 1]);
            minCost0 = minCost1;
            minCost1 = minCost;
        }
        return minCost;
    }
};

class Solution {
public:
    int minCostClimbingStairs(vector<int>& cost) {
        vector<int> dp(cost.size());
        dp[0] = cost[0];
        dp[1] = cost[1];
        for (int i = 2; i < cost.size(); i++) {
            dp[i] = min(dp[i - 2], dp[i - 1]) + cost[i];
        }
        return min(dp[cost.size() - 2], dp[cost.size() - 1]);
    }
};

//优化
class Solution {
public:
    int minCostClimbingStairs(vector<int>& cost) {
        for (int i = 2; i < cost.size(); i++) {
            cost[i] = min(cost[i - 2], cost[i - 1]) + cost[i];
        }
        return min(cost[cost.size() - 2], cost[cost.size() - 1]);
    }
};

4.leetcode 62. 不同路径

//4.leetcode 62. 不同路径

class Solution {
public:
    int uniquePaths(int m, int n) {
        vector<vector<int>> dp(m,vector<int>(n, 0));

        for (int i = 0; i < m; i++) {
            dp[i][0] = 1;
        }
        for (int j = 0; j < n; j++) {
            dp[0][j] = 1;
        }

        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
            }
        }
        return dp[m - 1][n - 1];
    }
};

5. leetcode 63 不同路径 II

//5. leetcode 63 不同路径 II

class Solution {
public:
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
        int m = obstacleGrid.size();
        int n = obstacleGrid[0].size();
        vector<vector<int>> dp(m, vector<int>(n, 0));

        for (int i = 0; i < m && obstacleGrid[i][0] == 0; i++) {
            dp[i][0] = 1;
        }
        for (int j = 0; j < n && obstacleGrid[0][j] == 0; j++) {
            dp[0][j] = 1;
        }

        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                if (obstacleGrid[i][j] == 1) {
                    continue;
                }
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
            }
        }
        return dp[m - 1][n - 1];
    }
};

6. leetcode 343 整数拆分

//6. leetcode 343 整数拆分

class Solution {
public:
    int integerBreak(int n) {
        vector<int> dp(n + 1);
        dp[2] = 1;
        for (int i = 3; i <= n; i++) {
            for (int j = 1; j < i - 1; j++) {
                dp[i] = max(dp[i], max((i - j) * j,dp[i - j] * j));
            }
        }
        return dp[n];
    }
};

7. leetcode 96 不同的二叉搜索树

//7. leetcode 96 不同的二叉搜索树

class Solution {
public:
    int numTrees(int n) {
        vector<int> dp(n + 1);
        dp[0] = 1;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                dp[i] += dp[j - 1] * dp[i - j];
            }
        }
        return dp[n];
    }
};

#endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值