Dynamic Planning and Applications in leetcode

Source: Internet
Author: User

Previously, we only knew that dynamic planning was to solve the original problem by combining sub-problems. However, it was always confusing how to analyze and apply it. Recently, we found several questions in leetcode that can be solved using dynamic planning.

Dynamic Planning: When subproblems overlap, multiple subproblems of the original issue may contain the same subproblems. Of course, the idea of breaking the original issue into subproblems is as follows, the splitting algorithm is also feasible, but some problems may occur if it is solved by recursion. In the calculation of repeated sub-problems, the division algorithm ignores the repetition problem, that is, the same problem, the division algorithm is calculated multiple times, so the efficiency will be very low. The dynamic planning algorithm will carefully arrange the order of solution, solve each subproblem only once, and keep the results. In this way, when a duplicate problem occurs, you only need to find and save the results without re-computing.

Dynamic Planning has two equivalent implementation methods:

1. top-down method with Memo.

2. bottom-up method.

Leetcode --- decode ways:

A message containing letters fromA-ZIs being encoded to numbers using the following mapping:

‘A‘ -> 1‘B‘ -> 2...‘Z‘ -> 26

Given an encoded message containing digits, determine the total number of ways to decode it.

For example,
Given encoded message"12", It cocould be decoded"AB"(1 2) or"L"(12 ).

The number of ways Decoding"12"Is 2.

Analysis:

Boundary Condition:

1. When the input string length is 0, it is 0

2. The first number of input strings cannot be 0. If it is 0, it is 0.

According to the example we can know the problem can be divided into sum [I] = sum [I-1] + sum [I-2] (I> 1 ).

We can use the bottom-up method to solve sub-problems easily.

The Code is as follows:

Public int numdecodings (string s) {If (0 = S. length () return 0; If (S. charat (0) = '0') return 0; int [] num = new int [S. length ()]; // record the status when traversing to the position I OF THE STRING (this status refers to the number of encoding methods) num [0] = 1; for (INT I = 1; I <S. length (); I ++) {If (S. charat (I )! = '0') num [I] = 1; string temp = S. substring (I-1, I + 1); If (temp. charat (0) = '0') continue; If (integer. parseint (temp)> 0 & integer. parseint (temp) <27) {if (1 = I) {num [I] + = 1 ;} else {num [I] + = num [I-2] ;}} return num [S. length ()-1];}

Of course, leetcode not only uses the idea of dynamic planning, but will summarize other questions in the future.

Dynamic Planning and Applications in leetcode

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.