Dynamic programming of five classical algorithms

Source: Internet
Author: User

First, the concept of origin

?? Dynamic programming, aka DP algorithm (derived from its dynamic programming abbreviation), is initially a branch of operations research and is a mathematical method used to solve the optimal decision-making process.

Second, the basic idea

?? The multi-stage process is transformed into a series of single-stage processes, which are solved by the relationship between the stages. What is the multi-stage process?

Multi-stage process : First of all, you can think about the following:

If we have several coins of $1/$ 3/$ 5, how can we make up to $137 with the fewest coins?

Of course we can use brute force enumeration to solve this problem, not enough complexity is too high. We can think of this, the 137 yuan can be regarded as a final goal, we can subdivide it to a minimum number of coins to gather 136 yuan (this adds 1 yuan to 137 yuan) or 133 yuan or 132 Yuan + 1 times. Then our problem turns out to be 136 yuan or 133 yuan or 132 yuan in the minimum number of coins. It seems that the number of problems has changed a lot, but the actual problem has become less difficult.
And this subdivision can be subdivided continuously, has been subdivided to close to 0 yuan. In this thinking process, we will solve the problem of 137 of dollars in a phased completion, and this process is called the multi-stage process .

Third, the problem-solving steps (Ideas)
    1. Using the idea of dynamic programming to think from top to bottom: Transforming a multi-stage problem into a smaller multi-stage problem (state transition equation)
    2. Decomposition to the smallest single-stage problem (which solves the problem directly).
    3. Use loops to solve problems from the bottom up.
Iv. framework of Algorithms

Compared with other basic algorithms, the dynamic programming algorithm is flexible, its main frame depends on its specific problem, the specific problem determines the specific state transfer equation; therefore, it is not like backtracking a set of "immutable" algorithm framework; so the following algorithm can only be said to solve similar coin problem of the DP algorithm framework, Can only be regarded as a catalyst for you.

? Variable interpretation:

?? Res: Storing answers to all stages of a problem

?? N: The mark bit of the final question

?? I: Indexing of loops

?? F: A functional relationship between the answer to a stage question and the answer to a previous stage question

void dp(int n) {  // 定义问题的解数组  int res[n + 1];  // 初始化最小的单阶段问题的解  res[1] = 1 ...  // 从初始化后的解数组的第一个位置开始循环计算res[i]  int i = inital;  while (i <= n) {    // f函数取决于状态转移方程    res[i] = f(res[i - 1], res[i - 2], res[i - 3]...);    i++;  }  return res[n];}  
V. Classical realization

Classic question: best time to Buy and Sell Stock

Say you had an array for which the ith elem ENT is the price of a given-stock on day I.

If You were-permitted to complete at most one transaction (i.e., b Uy One and sell one share of the the stock), design an algorithm to find the maximum profit.

Note that you cannot sell a stock before you buy one.

Example 1:

Input: [7,1,5,3,6,4]

Output:5

Explanation:buy on day 2 (price = 1) and sell on day 5 (Price = 6), Profit = 6-1 = 5.
Not 7-1 = 6, as selling price needs to being larger than buying price.
Example 2:
Input: [7,6,4,3,1]
output:0
Explanation:in This case, no transaction are done, i.e. Max Profit = 0.

int maxProfit(int* prices, int pricesSize) {  if (pricesSize == 0) {    return 0;  }  int res[pricesSize];  int min[pricesSize];  res[0] = 0;  min[0] = prices[0];  int i = 1;  while (i < pricesSize) {    if (res[i - 1] < prices[i] - min[i - 1]) {      res[i] = prices[i] - min[i - 1];    } else {      res[i] = res[i - 1];    }    if (prices[i] < min[i - 1]) {      min[i] = prices[i];    } else {      min[i] = min[i - 1];    }    i++;  }  return res[pricesSize - 1];}

Dynamic programming of five classical algorithms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.