Use Leetcode to familiarize yourself with dynamic planning

Source: Internet
Author: User

The purpose of this article is to review the topic of consolidating dynamic programming types, and will update the blog recently, and this article is suitable for the dynamic planning is not very familiar with the hope of reviewing dynamic planning. Dynamic Planning Steps

General thoughts on solving the problem of dynamic regulation

decompose the original problem into sub-problems

The original problem is decomposed into several sub-problems, the sub-problem and the original problem form the same or similar, but the size becomes smaller. Sub-problems are solved, the original problem is solved (digital triangle example).
Once the solution of a sub-problem is found, it is saved, so each sub-problem is solved only once.

Determine status

When solving problems with dynamic programming, we often refer to a set of variables related to sub-problems as a "state". A "state" corresponds to one or more sub-problems, the so-called "state" under the "value" is the "state" corresponding to the sub-problem of the solution.
A collection of all "states" that constitute the "state space" of the problem. The size of the state space is directly related to the time complexity of solving the problem with dynamic planning.

determine the value of some initial state (boundary State)

determining the state transition equation Leetcode Simple Topic Introduction Dynamic Planning

public class Dp_algorithm {//================= Dynamic planning first question =====================================///** * ======== * ======================== Topic ========================= * 198. House Robber * is a professional robber planning to rob houses along a street. Each house have a certain amount of money stashed, the only constraint stopping all from robbing each of the them are that Adjac ENT houses have security system connected and it would automatically contact the police if the adjacent houses were broken
     Into on the same night.  * *given a list of non-negative integers representing the amount of money in each house, determine the maximum amount
     The money you can rob tonight without alerting the police. * * * * */** * Solution One: * Brute Force search * We assume the first step from the last. * There are two ways to do this: * n-1 is the second step only (N-3,n-4,....
     * N-2 The second step only (N-4,,n-5,...)
        * */public static int solve1 (int idx,int[] nums) {if (idx<0) {return 0;
}        Return Math.max (Nums[idx]+solve1 (idx-2,nums), solve1 (idx-1,nums)); }/** * Solution II: * Algorithm after optimization * We assume that the first step from the last one is two ways of thinking: * N-1 is the second step only (N-3,n-4,....
     * N-2 The second step only (N-4,,n-5,...) * By observing we can see n-4,n-5 repeated calculations, then we will save the calculated results, * So save time * *///public static int[] public static int Sol
        Ve2 (int idx,int[] nums) {if (idx<0) {return 0;
        } if (results[idx]>0) {return RESULTS[IDX];
        } Results[idx] = Math.max (Nums[idx]+solve2 (idx-2,nums), Solve2 (idx-1,nums));
    return RESULTS[IDX];
    } static int[] results;
        public static void Main (string[] args) {int[] nums = new Int[3];
        Nums[0] = 1;
        NUMS[1] = 2;
        NUMS[2] = 3;
        results = new Int[nums.length];
        for (int i = 0; i < nums.length; i++) {Results[i] =-1;
     } int result = Solve2 (nums.length-1, nums);   System.out.print (result); }

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: 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.