ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Leetcode solution 322: Coin Change

2019-12-08 09:01:10  阅读:322  来源: 互联网

标签:coin min int coins 322 amount lookup Coin Leetcode


Problem Statement 

You are given coins of different denominations and a total amount of money amount. 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:

Input: coins = [1, 2, 5], amount = 11
Output: 3 
Explanation: 11 = 5 + 5 + 1

Example 2:

Input: coins = [2], amount = 3
Output: -1
 

Problem link

Video Tutorial

You can find the detailed video tutorial here

  • Youtube
  • B站

 

Thought Process

This is a classic problem and greedy might be the first thing comes into mind. For example, sort the coins on denomination and always use the largest amount coin. However, the optimal solution might not include the largest denomination.

 

for example , [1, 3, 4] and target value 6. The optimal is two 3s, not [4, 1, 1].

 

It seems we have to list out all the different combinations and find the min value, that leads to a brute fore solution as described here using recursion. It would be an exponential time complexity.

 

We can use a hash map as a lookup to remember for each amount, what would be the min cost. That would significantly reduce the time complexity from exponential to O(S*N) where S is the amount and N is the coins array size. It's not obvious why but once you see the DP solution, you can understand better, it's the same time complexity.

 

Another way is to use dynamic programming because the problem is asking for a single yes/no, extreme value(min, max), building up a lookup table using formula is the coding pattern. We have similar problems, Wild Card Matching, Regular Expression Matching

 

The idea is to build a lookup table for each amount, what's the minimal number of coins needed given current  denomination.

lookup[i] = min(lookup[i], lookup[i - coin value] + 1) given i - coin value >= 0

Solutions

Dynamic Programming

 1 public int coinChangeDP(int[] coins, int amount) {
 2     if (coins == null || coins.length == 0) {
 3         return -1;
 4     }
 5     // lookup contains the min number of coins needed to reach the amount(i.e., index)
 6     int[] lookup = new int[amount + 1];
 7     lookup[0] = 0;
 8 
 9     for (int i = 1; i <= amount; i++) {
10         int min = Integer.MAX_VALUE;
11         for (int coin : coins) {
12             if (i >= coin && lookup[i - coin] != -1) {
13                 min = Math.min(min, lookup[i - coin] + 1);
14             }
15         }
16         lookup[i] = (min == Integer.MAX_VALUE ? -1 : min);
17     }
18 
19     return lookup[amount];
20 }

 

Time Complexity: O(S*N) where S is the amount and N is the coins array size

Space Complexity: O(N) since we used a lookup array

 

References

标签:coin,min,int,coins,322,amount,lookup,Coin,Leetcode
来源: https://www.cnblogs.com/baozitraining/p/12004581.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有