Dynamic Programming Getting Started

More welcome attention to micro-channel public number: engineer Xiaohui whole dish. No reply to the public keyword, receive free learning materials.

Dynamic programming algorithm has been interviewing Shredded algorithms are more challenging type. Many distribution problems or scheduling problems can in fact use dynamic programming to resolve. (Of course, if the larger scale of the problem, sometimes abstract model using a dynamic return to resolve, sometimes you can find second-best solution algorithm to solve the probability we continue iteration)

So move return is important, at least algorithm thinking is very important.

What is dynamic programming?

The method by the original problem into sub-problems of a relatively simple way to solve complex problems. Dynamic programming is often suitable for overlapping sub-optimal substructure property issues and problems.

Optimal substructure: When the optimal solution contains a sub-optimal solution of its problems, said the problem has structural sub-optimal.

Overlapping sub-problems: when a top-down recursive algorithm Solutions, each sub-problem are not always produced new problems, some sub-problems are of iterations. Dynamic programming algorithm is the use of the overlapping nature of this sub-problems, the solution only once for each sub-problem, and its solution will save a table, after the solution of these sub-problems using as much as possible.

Do not be afraid do not understand, combined with the back of the subject to understand these concepts. These concepts have been entirely owned by the people will move summed up, so go first understand the movement, and then look at generalize these genteel.

Divide and Conquer and Dynamic Programming

Thing in common:
both require the original problem has a sub-optimal structural properties, are the original problem of divide and rule, broken down into a number of smaller-scale (small to easily solved) sub-problems. Then the solution of problems of sub-merged, forming a solution of the original problem.

difference:

  • Sub-divide and conquer problem decomposed as independent, done by recursively.
  • The dynamic programming sub-problems decomposed understood to have contact with each other, overlapping part, need to remember, usually an iterative do.

Step Dynamic Programming

Modeling problems

  1. According to the problem, find the optimal substructure []. The original question from Dahua small first step, to find the best results to be less than the current number one issue, but under normal circumstances the current problem can be represented by a sub-optimal structure.
  2. [Border] problem determination. According to the above-mentioned optimal substructure, step by step, from small Dahua can eventually obtain the minimum, optimal substructure can see at a glance the answer, that is the border.
  3. Through the above two steps, by analyzing the relationship between optimal substructure and final question, we can get the state transition equation [].

    Various methods of problem-solving

    Violence enumeration:

    All dynamic programming problems can be through multiple levels of nested loop through all of the possible, the number of eligible statistics up. Just time complexity is exponential, it is not recommended.

Recursion:

  1. Recursive time complexity is determined by the number of layers and the optimal recursive substructures.
  2. In climbing the ladder problem, at least looking for change problem, the recursive time complexity and space complexity than the difference between the dynamic normalization process, but problems with the king's gold, different data scale, time dynamic complexity of return method and space complexity is not necessarily better than recursive. Therefore, to analyze specific issues.

Three issues mentioned above are very common dynamic programming in the title, the title content can look at Baidu. Space reasons, this article deals only behind the first two questions

Memorandum algorithm:

  1. In the N number of steps more time on the shortcomings revealed recursive algorithm: the time complexity is very high. If you draw a recurrence plot (like as a binary tree), you will find there are many, many duplicate nodes. However, the conventional recursive algorithm does not recognize the node is not repeated, as long as less than the termination condition, it would have been recursion.
  2. To avoid this, the recursive algorithm can not be repeated recursively, has been put nodes are kept up, when the next time you encounter a direct result kept up with on the line. This is the memo algorithm.
  3. Memorandum of time and space complexity of the algorithm complexity has been simplified.

Dynamic programming algorithm:

  1. The above-mentioned memorandum algorithm, although has been pretty good, but they are still from the original problem, traversing get all of the most kid problem, space complexity is O (N).
  2. In order to reduce the space complexity, we can construct a recursive bottom-up issue again, by analyzing the relationship between optimal substructure and final question, we can get the state transition equation [].
    Then upward and iterations from the smallest problem, even up to the maximum iteration of the original problem, but also rely only on a few optimal substructure front. In this way, space complexity is greatly simplified. It has been owned by the algorithm dynamic algorithm.

example

Example 1: Climbing Stairs (stair climbing problems)
leetcode original title : You're climbing a staircase has n steps, each time only on one or two steps, then get to the top how many different ways there are?

  1. Modeling:
  • The final problem F (N): from 0 to reach the first method assumes that the N total step F (N) number.
  • Optimal substructure F (N-1), F (N-2): N stepped arrival, there are two possibilities, the first possibility is the first step the N-1 steps to reach the terminal 1, a second We may be reaching the end of the N-2 from step 2 th step.
  • Optimal child relationship between structure and final problems: According to the above expression, it can be summarized F (N) = F (N-1) + F (N-2) (n> = 3)
  • Border: F (1) = 1, F (2) = 2
  1. Problem Solving:
  • Recursion:
class Solution {
    int climbStairs(int n) {
        if (n <= 2) {
            return n;
        } else {
            return climbStairs(n - 1) + climbStairs(n - 2);
        }
    }
}

Recursive time complexity is determined by the number of layers and the optimal recursive substructures. Here the number of steps is N, the number of optimal substructure is 2. If you think of a binary tree, then it can be considered to be a highly N-1, N-1 number of nodes close to the power of 2 of the tree, the time complexity of this method may be regarded as approximately O (2 N ) .

  • Memorandum algorithm:
    Here we think of the duplicate parameters stored, the next recursive return directly encountered the results of the parameters, that is, the algorithm memo, the memo is the most simple hash table.
class Solution {
    private Map<Integer, Integer> map = new HashMap<>();

    int climbStairs(int n) {
        if (n <= 2) {
            return n;
        } else if (map.containsKey(n)) {
            return map.get(n);
        } else {
            int value = climbStairs(n - 1) + climbStairs(n - 2);
            map.put(n, value);
            return value;
        }
    }
}
  • Dynamic programming:
    Before solving are top-down, bottom-up consider the solution process. From F (1) and F (2) the boundary condition evaluation, found that F (3) = F (1 ) + F (2). Continue upward, it is understood F (N) is only dependent on the previous state of the two F (N-1) and F (N-2). So we just need to keep the first two states, we can obtain F (N). Compared to the memorandum algorithm, we once again simplify the spatial complexity.
class Solution {
    int climbStairs(int n) {
        if (n <= 2) {
            return n;
        }
        // 边界条件
        int a = 1;
        int b = 2;
        int result = 0;
        // 最优子结构与最终问题之间的关系
        for (int i = 3; i <= n; i++) {
            result = a + b;
            a = b;
            b = result;
        }
        return result;
    }
}

Space complexity O (1), the time complexity of O (N)

Example 2: Making change using the fewest coins ( at least looking for change problem)
Google interview questions : Suppose you are a manufacturer of vending machine programmer. Your company is trying to be able to provide a minimum number of coins in the change for each transaction in order to be able to work easier. There are four known coins (1 cent, 5 cents, 10 cents, 25 cents). Suppose a customer investment of $ 1 to 37 cents purchase items, the minimum number you used to the change of the coin is how much?

  1. Modeling:
  • Optimal substructure: Recall find a way to optimal substructure, it is a step back, the best results can be obtained. There are four options, 1 + mincoins (63-1), 1 + mincoins (63-5), 1 + mincoins (63-10) or 1 + mincoins (63-25), which can be considered four options 63 the optimal substructure.
  • State transition equation: according to the above-described optimal substructure, mincoins (63) is the same as the optimal minimum value of the four substructures.
  • Boundaries: When the required amount exactly equal to the change in the hands of a single denomination coins, you can return 1.
  1. Problem Solving:
  • Recursion:
class Solution {
    Set<Integer> coinSet = new HashSet<Integer>() {
        {
            add(1);
            add(5);
            add(10);
            add(25);
        }
    };

    int getFewestCoins(int n) {
        if (n < 1) {
            return 0;
        }
        if (coinSet.contains(n)) {
            return 1;
        }
        int minCoins = n;
        int numCoins = Integer.MAX_VALUE;

        for (int coin : coinSet) {
            if (n >= coin) {
                // 如果要计算的n小于单个硬币金额,则不能出现在状态转移方程中
                numCoins = 1 + getFewestCoins(n - coin);
            }
            // 更新最小值
            if (numCoins < minCoins) {
                minCoins = numCoins;
            }
        }
        return minCoins;
    }
}
  • Memorandum algorithm:
    that is, the intermediate variables in the recursive calculations are stored in a hash table, the code slightly.

  • Dynamic Programming:
    bottom-up, a start up from the iteration number is equal to the change, with reference to optimal substructure, record the minimum number of coins. It has been iteration to the actual requirements.

class Solution {
    Set<Integer> coinSet = new HashSet<Integer>() {
        {
            add(1);
            add(5);
            add(10);
            add(25);
        }
    };

    int getFewestCoins(int n) {
        int[] list = new int[n + 1];
        List<Integer> subCal = new ArrayList<>();
        for (int i = 0; i <= n; i++) {
            // 边界
            if (i <= 1) {
                list[i] = i;
                continue;
            }
            for (int cent : coinSet) {
                if (i >= cent) {
                    subCal.add(list[i - cent] + 1);
                }
            }
            list[i] = Collections.min(subCal);
            subCal.clear();
        }
        return list[n];
    }
}

More welcome attention to micro-channel public number: engineer Xiaohui whole dish. No reply to the public keyword, receive free learning materials.

Heck, if my card lost.  Micro-letter search for "whole food engineer Xiaohui," I can still find

Guess you like

Origin www.cnblogs.com/mseddl/p/11433030.html