分而治之与动态规划

https://stackoverflow.com/questions/13538459/difference-between-divide-and-conquer-algo-and-dynamic-programming

关于这个非常著名的问题,stackoverflow这里给予了一个比较好的解释。

Divide and Conquer

Divide and Conquer works by dividing the problem into sub-problems, conquer each sub-problem recursively and combine these solutions.

Dynamic Programming

Dynamic Programming is a technique for solving problems with overlapping subproblems. Each sub-problem is solved only once and the result of each sub-problem is stored in a table ( generally implemented as an array or a hash table) for future references. These sub-solutions may be used to obtain the original solution and the technique of storing the sub-problem solutions is known as memoization.

You may think of DP = "recursion + re-use".
DP also = "controlled brute force".

A classic example to understand the difference would be to see both these approaches towards obtaining the nth fibonacci number. Check this material from MIT.

这个也是DP, 但是并不是标准的递归使用:

class Solution
{
public:
    int maxProfit(vector<int> &prices)
    {
        if (prices.size() < 2)
            return 0;
        int Min = prices[0], Max = 0;
        for (int i = 1; i < prices.size(); i++)
        {
            Max = max(Max, prices[i] - Min);
            Min = min(Min, prices[i]);
        }
        return Max;
    }
};
posted @ 2021-03-07 16:09  千心  阅读(66)  评论(0编辑  收藏  举报