322. Coin Change

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/t6_17/article/details/78785874

Coin Change

题目描述

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:
coins = [1, 2, 5], amount = 11
return 3 (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.
这是动态规划最经典的一个问题,找出状态转移方程就不难了,dp表示能找到当前零钱的最少硬币数量。

dp[i] = min(dp[i], dp[i - coins[j]] + 1)

解题思路

class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        int Max = amount + 1;
        vector<int> dp(amount + 1, Max);
        dp[0] = 0;
        for (int i = 1; i <= amount; i++) {
            for (int j = 0; j < coins.size(); j++) {
                if (coins[j] <= i) {
                    dp[i] = min(dp[i], dp[i - coins[j]] + 1);
                }
            }
        }
        return dp[amount] > amount ? -1 : dp[amount];
    }
};
展开阅读全文

求助高手,一个类似找钱(coin change)的算法

10-31

有那么一个问题,有一组数字和一个数字rnrn一组数字 5,10,15,25,50 rn一个数字 215rnrn问题是215能不能由 这一组数字中任意一些数字组成。rnrn比如215 = 8个25 + 1个15 组成rnrn 265 = 8个25 1个50 1个15组成rnrn现在的问题是,只要找到任意一个组成了就可以返回。。不用管到底有多少中组成, 如果找不到rnrn例如299, 最后结果就是不能组成。。rnrn我用的办法是递归+全局判断。。问题出现了。。当一组数字中个数大于4个。。数字大于2000的时候速度一下子变慢,特别是找不到组成的时候。。我看了下一共循环了几千外次。。rnrn比如 5,10,15,25,50 和 2011的时候,,运算了几十秒。。问问大侠有没有快速点的算法。。如果没有更好的算发能不能+一些判断提高速度。。rnrn那一组数字的个数可能是4-10个 (只要设定了就是确定的,基本不会修改)。。一个数字1-100000 (是随机的)。。。rnrn1. 提高速度的判断我就想到了求这几个数字的公约数。。看是不是能被2011整除。。如果不是的话就直接不能组成了rnrnrnstatic bool res = false;rnstatic int count = 0;rnstatic void findAllCombinationsRecursive(List notes, int startIx, int remainingTarget)rn rn for (int i = startIx; i < notes.Count && !res; i++)rn rn count++;rnrn int temp = remainingTarget - notes[i];rnrn if (temp < 0)rn rn break;rn rnrn if (temp == 0)rn rn // reached the answer hence quit from the looprn res = true;rn rn elsern rn // target not reached, try the solution recursively with thern // current denomination as the start point.rn findAllCombinationsRecursive(notes, i, temp);rn rn rn 论坛

False Coin

03-17

The "Gold Bar" bank received information from reliable sources that in their last group of N coins exactly one coin is false and differs in weight from other coins (while all other coins are equal in weight). After the economic crisis they have only a simple balance available (like one in the picture). Using this balance, one is able to determine if the weight of objects in the left pan is less than, greater than, or equal to the weight of objects in the right pan.nnIn order to detect the false coin the bank employees numbered all coins by the integers from 1 to N, thus assigning each coin a unique integer identifier. After that they began to weight various groups of coins by placing equal numbers of coins in the left pan and in the right pan. The identifiers of coins and the results of the weightings were carefully recorded.nnYou are to write a program that will help the bank employees to determine the identifier of the false coin using the results of these weightings.nnnInputnnThe first line of the input contains two integers N and K, separated by spaces, where N is the number of coins (2 <= N <= 100) and K is the number of weightings fulfilled (1 <= K <= 100). The following 2K lines describe all weightings. Two consecutive lines describe each weighting. The first of them starts with a number Pi (1 <= Pi <= N / 2), representing the number of coins placed in the left and in the right pans, followed by Pi identifiers of coins placed in the left pan and Pi identifiers of coins placed in the right pan. All numbers are separated by spaces. The second line contains one of the following characters: '<', '>', or '='. It represents the result of the weighting:nn'<' means that the weight of coins in the left pan is less than the weight of coins in the right pan,n'>' means that the weight of coins in the left pan is greater than the weight of coins in the right pan,n'=' means that the weight of coins in the left pan is equal to the weight of coins in the right pan.nnOutputnnWrite to the output the identifier of the false coin or 0, if it cannot be found by the results of the given weightings.nnnThis problem contains multiple test cases!nnThe first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.nnThe output format consists of N output blocks. There is a blank line between output blocks.nnSample Inputnn2nn5 3n2 1 2 3 4nnnnSample Outputnn3nn0n 问答

False coin

07-15

DescriptionnnThe "Gold Bar"bank received information from reliable sources that in their last group of N coins exactly one coin is false and differs in weight from other coins (while all other coins are equal in weight). After the economic crisis they have only a simple balance available (like one in the picture). Using this balance, one is able to determine if the weight of objects in the left pan is less than, greater than, or equal to the weight of objects in the right pan. nIn order to detect the false coin the bank employees numbered all coins by the integers from 1 to N, thus assigning each coin a unique integer identifier. After that they began to weight various groups of coins by placing equal numbers of coins in the left pan and in the right pan. The identifiers of coins and the results of the weightings were carefully recorded. nYou are to write a program that will help the bank employees to determine the identifier of the false coin using the results of these weightings. nInputnnThe first line of the input file contains two integers N and K, separated by spaces, where N is the number of coins (2<=N<=1000 ) and K is the number of weightings fulfilled (1<=K<=100). The following 2K lines describe all weightings. Two consecutive lines describe each weighting. The first of them starts with a number Pi (1<=Pi<=N/2), representing the number of coins placed in the left and in the right pans, followed by Pi identifiers of coins placed in the left pan and Pi identifiers of coins placed in the right pan. All numbers are separated by spaces. The second line contains one of the following characters: '<', '>', or '='. It represents the result of the weighting: n'<' means that the weight of coins in the left pan is less than the weight of coins in the right pan, n'>' means that the weight of coins in the left pan is greater than the weight of coins in the right pan, n'=' means that the weight of coins in the left pan is equal to the weight of coins in the right pan. nOutputnnWrite to the output file the identifier of the false coin or 0, if it cannot be found by the results of the given weightings.nSample Inputnn5 3n2 1 2 3 4n 问答

A Coin Game

08-17

DescriptionnnHarry and Sally have recently got addicted to a coin game. The game goes as follows.nnThe games uses a T × T grid board, each square in which is denoted by a pair of integers (x, y) between 1 and T (inclusive). Two players take turns to flip the coins. Each turn a player can choose four coins in the squares (x1, y1), (x1, y2), (x2, y1) and (x2, y2) (1 ≤ x1 < x2 ≤ T, 1 ≤ y1 < y2 ≤ T) to flip, as long as the coin in (x2, y2) will be turned from heads to tails. The player who cannot make a move loses.nnAfter a sweet winning streak, Harry says to Sally, “Let’s have a bet! I’ll play first, and I’ll put heads in some squares and tails in some others of the board. You can decide whether the rest of coins shows heads or tails. I dare say you can’t beat me no matter how you put them.”nnGiven the squares in which Harry puts heads and those in which he puts tails, can you help Sally decide on the rest of coins so that she will win to teach the arrogant Harry a lesson? Assume that both Harry and Sally play optimally in the game.nnInputnnThe input contains multiple test cases. Each test cases begins with a line containing two integers n1 and n2 (n1, n2 ≤ 200). The next n1 + n2 lines each contain a pair of integers (x, y) (1 ≤ x, y ≤ 100). For the first n1 pairs, Harry puts heads in the corresponding squares. Harry lets Sally decide on coins in the squares denoted by the rest n2 pairs and puts tails in other squares.nnOutputnnFor each test case, if Sally can win the game, print “Yes”, followed by n2 letters being either “H” (heads) or “T” (tails), showing one way (possibly among others) she put the coins. The output should be in the same order as the squares appear in the input. If Sally cannot win, just print “No”.nnSample Inputnn0 4n1 1n1 2n2 1n2 2n1 1n1 1n2 2nSample OutputnnYesnTnTnTnTnYesnT 问答

没有更多推荐了,返回首页