Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the problems of dynamic programming algorithm

Shulou Source: shulou.com Published: 2022-06-03 04:27:44 09月26日 Update

This article mainly introduces "what are the problems of the dynamic programming algorithm". In the daily operation, I believe that many people have doubts about the problems of the dynamic programming algorithm. The editor consulted all kinds of materials and sorted out the simple and easy-to-use operation methods. I hope it will be helpful for you to answer the questions of "what are the problems of dynamic programming algorithms?" Next, please follow the editor to study!

Problem: all values in a given array arr,arr are positive and do not repeat. Each value represents a denomination currency, each denomination currency can use any piece, and given an integer aim represents the amount of money you are looking for, there are many ways to change it.

Analysis on the method of violent search

Given arr= {5, 10, 25, 1}, aim=1000.

Use 0 pieces of 5 yuan currency, let [10,25,1] form the remaining 1000 yuan, and the final method is recorded as-res1

With a piece of 5 yuan currency, let [10,25,1] form the remaining 995 yuan, and the final method is recorded as-res2.

With two pieces of 5 yuan currency, let [10,25,1] form the remaining 990 yuan, and the final method is recorded as-res3.

With three pieces of 5 yuan currency, let [10,25,1] form the remaining 985 yuan, and the final method is recorded as-res4.

……

With 200 pieces of 5 yuan currency, let [10,25,1] form the remaining 0 yuan, and the final method number is recorded as-res201.

Then res1, res2. The sum of res201 is the final result.

Concrete realization

Define a recursive function: int process1 (arr, index, aim), which means that if you use arr [index... Nmur1] these denominations make up the aim and return the total number of methods.

Public static int coins1 (int [] arr, int aim) {long startTime = System.currentTimeMillis (); if (arr = = null | | arr.length = = 0 | | aim < 0) {return 0;} int result = process1 (arr,0,aim); long endTime = System.currentTimeMillis () System.out.println ("time spent by violent search method:" + (endTime-startTime) + "ms"); return result;} public static int process1 (int [] arr, int index, int aim) {int res = 0 / / determine whether all denomination currencies have been calculated if (index = = arr.length) {/ / determine whether the total amount of money in this recursive call has been enough, and if so, add the total number of methods to 1 res = aim = 0? 1: 0 } else {/ / circularly calculate I current denomination currency for (int I = 0; arr [index] * I

Tags: Method currency dynamics planning recursion search memory process time result violence algorithm problem face value matrix complexity complexity state order no Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL macOS Docker MariaDB OPPO Reno