Detailed explanation of the change problem of dynamic programming
Change problem: The amount of change required is W, the face values of coins are (d1, d2, d3,..., dm), how many coins are needed at least.
Question: The amount of change required is 8. The face values of coins are (1, 3, 2, 5). How many coins are needed at least.
Let F(j) represent the minimum number of change when the total amount is j, F(0) = 0, W represents the change amount, and there is a pile of change {d1, d2, d3,..., dm} . Also based on previous experience, to achieve j, it must be the number of coins with a face value of j – di(1 <= i <= m) plus 1 coin with a face value of di. Of course, j >= di, that is, F (j) = F(j - di) + 1, j >= di.
Java
1 package com.algorithm.dynamicprogramming; 2 3 import java.util.Arrays; 4 5 /** 6 * 找零问题 7 * Created by yulinfeng on 7/5/17. 8 */ 9 public class Money {10 public static void main(String[] args) {11 int[] money = {1, 3, 2, 5};12 int sum = 8;13 int count = money(money, sum);14 System.out.println(count);15 }16 17 private static int money(int[] money, int sum) {18 int[] count = new int[sum + 1];19 count[0] = 0;20 for (int j = 1; j < sum + 1; j++) { //总金额数,1,2,3,……,sum21 int minCoins = j;22 for (int i = 0; i < money.length; i++) { //遍历硬币的面值23 if (j - money[i] >= 0) {24 int temp = count[j - money[i]] + 1; //当前所需硬币数25 if (temp < minCoins) {26 minCoins = temp;27 }28 }29 }30 31 count[j] = minCoins;32 }33 System.out.println(Arrays.toString(count));34 return count[sum];35 }36 }
## Python3
1 #coding=utf-8 2 def charge_making(money, num): 3 ''' 4 找零问题 5 ''' 6 count = [0] * (num + 1) 7 count[0] = 0 8 for j in range(1, num + 1): 9 minCoins = j10 for i in range(len(money)):11 if j - money[i] >= 0:12 temp = count[j - money[i]] + 113 if temp < minCoins:14 minCoins = temp15 16 count[j] = minCoins17 18 return count[num]19 20 money = [1, 3, 2, 5]21 num = 822 count = charge_making(money, num)23 print(count)
tag
The above is the detailed content of Detailed explanation of the change problem of dynamic programming. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The clustering effect evaluation problem in the clustering algorithm requires specific code examples. Clustering is an unsupervised learning method that groups similar samples into one category by clustering data. In clustering algorithms, how to evaluate the effect of clustering is an important issue. This article will introduce several commonly used clustering effect evaluation indicators and give corresponding code examples. 1. Clustering effect evaluation index Silhouette Coefficient Silhouette coefficient evaluates the clustering effect by calculating the closeness of the sample and the degree of separation from other clusters.

Solve the "error:redefinitionofclass'ClassName'" problem in C++ code. In C++ programming, we often encounter various compilation errors. One of the common errors is "error:redefinitionofclass 'ClassName'" (redefinition error of class 'ClassName'). This error usually occurs when the same class is defined multiple times. This article will

How to use C# to write dynamic programming algorithm Summary: Dynamic programming is a common algorithm for solving optimization problems and is suitable for a variety of scenarios. This article will introduce how to use C# to write dynamic programming algorithms and provide specific code examples. 1. What is a dynamic programming algorithm? Dynamic Programming (DP) is an algorithmic idea used to solve problems with overlapping subproblems and optimal substructure properties. Dynamic programming decomposes the problem into several sub-problems to solve, and records the solution to each sub-problem.

Known for its powerful performance and versatile features, the iPhone is not immune to the occasional hiccup or technical difficulty, a common trait among complex electronic devices. Experiencing iPhone problems can be frustrating, but usually no alarm is needed. In this comprehensive guide, we aim to demystify some of the most commonly encountered challenges associated with iPhone usage. Our step-by-step approach is designed to help you resolve these common issues, providing practical solutions and troubleshooting tips to get your equipment back in peak working order. Whether you're facing a glitch or a more complex problem, this article can help you resolve them effectively. General Troubleshooting Tips Before delving into specific troubleshooting steps, here are some helpful

PHP algorithm analysis: How to use dynamic programming algorithm to solve the longest palindrome substring problem? Dynamic Programming (Dynamic Programming) is a commonly used algorithm idea that can solve many complex problems. One of them is the longest palindrome substring problem, which is to find the length of the longest palindrome substring in a string. This article will introduce how to use PHP to write a dynamic programming algorithm to solve this problem, and provide specific code examples. Let’s first define the longest palindrome substring. A palindrome string refers to a string that reads the same forward and backward, and the palindrome string

How to use the knapsack problem algorithm in C++ The knapsack problem is one of the classic problems in computer algorithms. It involves how to select some items to put into the knapsack under a given knapsack capacity to maximize the total value of the items. This article will introduce in detail how to use the dynamic programming algorithm in C++ to solve the knapsack problem, and give specific code examples. First, we need to define the input and output of the knapsack problem. The input includes the weight array wt[] of the item, the value array val[] of the item, and the capacity W of the backpack. The output is which objects are selected

To solve the problem that jQuery.val() cannot be used, specific code examples are required. For front-end developers, using jQuery is one of the common operations. Among them, using the .val() method to get or set the value of a form element is a very common operation. However, in some specific cases, the problem of not being able to use the .val() method may arise. This article will introduce some common situations and solutions, and provide specific code examples. Problem Description When using jQuery to develop front-end pages, sometimes you will encounter

The label acquisition problem in weakly supervised learning requires specific code examples. Introduction: Weakly supervised learning is a machine learning method that uses weak labels for training. Different from traditional supervised learning, weakly supervised learning only needs to use fewer labels to train the model, rather than each sample needs to have an accurate label. However, in weakly supervised learning, how to accurately obtain useful information from weak labels is a key issue. This article will introduce the label acquisition problem in weakly supervised learning and give specific code examples. Introduction to the label acquisition problem in weakly supervised learning:
