LeetCode OJ 322. Coin Change DP Solution

Source: Internet
Author: User

LeetCode OJ 322. Coin Change DP Solution
322. Coin ChangeMy SubmissionsQuestionTotal Accepted: 15289 Total Submissions: 62250 Difficulty: Medium

You are given coins of different denominations and a total amount of moneyamount. write a function to compute the fewest number of coins that you need to make up that amount. if that amount of money cannot be made up by any combination of the coins, return-1.

Example 1:
Coins =[1, 2, 5], Amount =11
Return3(11 = 5 + 5 + 1)

Example 2:
Coins =[2], Amount =3
Return-1.

Note:
You may assume that you have an infinite number of each kind of coin.

Credits:
Special thanks [email protected] adding this problem and creating all test cases.

Subscribeto see which companies asked this question

Show TagsHave you met this question in a real interview? YesNo

Discuss

A number of coins with fixed nominal values can be used infinitely. The number of targets, which must be exchanged with the minimum number of coins.

Another way of thinking is to understand the question. Each time you can take the number of steps given the nominal value and ask how many steps can be taken to reach the goal. In this way, you can use BFS to solve the problem.

The second solution is DP, dp [I] = min {dp [I-a], dp [I-B], dp [I-c]...}. Dynamic Planning can be quickly solved with formulas.

The AC code I solved using DP

 

Package march; import java. util. arrays;/*** @ auther lvsheng * @ date July 15, March 16, 2016 * @ time 11:20:44 * @ project LeetCodeOJ **/public class CoinChange {public static void main (String [] args) {int [] a = {1, 2, 5}; System. out. println (coinChange (a, 11); int [] B = {2}; System. out. println (coinChange (B, 3);} public static int coinChange (int [] coins, int amount) {int len = coins. length; if (len = 0) return -1; int [] dp = new int [amount + 1]; Arrays. fill (dp, Integer. MAX_VALUE); dp [0] = 0; Arrays. sort (coins); for (int I = 0; I <len & coins [I] <= amount; I ++) dp [coins [I] = 1; for (int I = 1; I <= amount; I ++) {int min = dp [I]; if (min = 1) continue; for (int j = 0; j <len & coins [j] <I; j ++) {int c = coins [j]; int d = dp [I-c]; if (d! = Integer. MAX_VALUE & d <min) min = d + 1;} dp [I] = min;} return dp [amount] = Integer. MAX_VALUE? -1: dp [amount];}

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.