Dynamic programming algorithm [Title]

Dynamic programming is generally divided into three categories of issues:

  1. Count
    - how many ways come to the lower right corner
    - How many ways and are selected such that the number k Sum
  2. Maximum and minimum
    - went from the top left bottom right path and the maximum number of
    - rising longest sequence length
  3. Existence of
    - taking stones game, if the upper hand win
    - can and are selected such that the number k Sum

Problem-solving ideas usually need to be bottom-up and top-down to design.

Example:
.. Give different denominations of coins and a total amount of write a method to calculate the total amount given the number of coins can exchange the least if there were any combination of coins can not be equal to the total amount of face value, then return -1 .

Sample

Sample 1

输入:
[1, 2, 5]
11
输出: 3
解释: 11 = 5 + 5 + 1

Sample 2

输入: 
[2]
3
输出: -1

Solving steps as follows (Example 1 sample):

  1. Determine the status.
    The final step Optimal Strategy (last coin optimal strategy used \ (a_k \) ), into sub-problems (the least coin spell smaller face value \ (11-a_k \) ).
  2. Transfer equation.
    \ [Dp [X] = min \ {dp [X-1] + 1, dp [X-2] + 1, dp [X-5] +1 \} \]
  3. The initial and boundary conditions.
    $ Dp [0] = 0 $ , if not spell the Y,
    $ DP [the Y] = $ INTMAX
  4. Calculation procedure. Calculated using the results of previous good.
    $ Dp [0], dp [ 1], dp [2] ... $

Guess you like

Origin www.cnblogs.com/lvjincheng/p/11360574.html