//
// coin.c
// coinDP
//
// Created by vaibhav gautam on 11/28/14.
// Copyright (c) 2014 vaibhav gautam. All rights reserved.
//
// Given a list of N coins, their values (V1, V2, ... , VN),
// and the total sum S. Find the minimum number of coins the sum of which is
// S (we can use as many coins of one type as we want), or report that it's
// not possible to select coins in such a way that they sum up to S.
#include <stdio.h>
#include <stdint.h>
int mincount(int change[], int changeSize, int SUM)
{
int table[SUM + 1];
table[0] = 0;
int min = INT32_MAX;
int sum = 0;
int i = 0;
int j = 0;
for (sum = 1; sum <= SUM; sum++) {
for (j = 0; j < changeSize; j++) {
// Pick first coin and substract with the sum
// Check if sum is less than 0
if (sum - change[j] < 0) {
continue;
} else if (sum - change[j] == 0) {
// This is the case when sum is either 1, 5, 10, 25
// In this case mininum number of coin required is 1
min = 1;
break;
}
// This is case when we say sum is 3
// In this case lets start with first coint which is 1
// If we choose coint 1 then the sum left os 3 - 1 = 2
// Given we are calculating for sum 3 means we have already
// calculated for sum 2. So go to table for sum = 2 and
// get the min number of ways sum 2 is computed.
// Here i is the sum i.e. lets say as per our example
// i = 3, j = 0
// 1 + table[3 - change[0]];
// 1 + table[3 - 1];
// 1 + table[2];
// 1 + 2 = 3
// Sum 3 requires at least 3 coins {1, 1, 1}
if (min > (1 + table[sum - change[j]])) {
min = 1 + table[sum - change[j]];
}
}
table[sum] = min;
min = INT32_MAX;
}
for (i = 0; i <= SUM; i++) {
printf("SUM[%d]: %d\n", i
, table
[i
]); }
return table[SUM];
}
int main(int argc, const char * argv[])
{
int change[4] = {1, 5, 10, 25};
int changeSize = sizeof(change)/sizeof(change[0]);
int SUM = 16;
printf("MinChange for sum %d = %d\n", SUM
, mincount
(change
, changeSize
, SUM
));
return 0;
}
Ly8KLy8gIGNvaW4uYwovLyAgY29pbkRQCi8vCi8vICBDcmVhdGVkIGJ5IHZhaWJoYXYgZ2F1dGFtIG9uIDExLzI4LzE0LgovLyAgQ29weXJpZ2h0IChjKSAyMDE0IHZhaWJoYXYgZ2F1dGFtLiB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AgICAgICAgIC8vIFRoaXMgaXMgdGhlIGNhc2Ugd2hlbiBzdW0gaXMgZWl0aGVyIDEsIDUsIDEwLCAyNQogICAgICAgICAgICAgICAgLy8gSW4gdGhpcyBjYXNlIG1pbmludW0gbnVtYmVyIG9mIGNvaW4gcmVxdWlyZWQgaXMgMQogICAgICAgICAgICAgICAgbWluID0gMTsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICB9CiAgICAgICAgICAgIAogICAgICAgICAgICAvLyBUaGlzIGlzIGNhc2Ugd2hlbiB3ZSBzYXkgc3VtIGlzIDMKICAgICAgICAgICAgLy8gSW4gdGhpcyBjYXNlIGxldHMgc3RhcnQgd2l0aCBmaXJzdCBjb2ludCB3aGljaCBpcyAxCiAgICAgICAgICAgIC8vIElmIHdlIGNob29zZSBjb2ludCAxIHRoZW4gdGhlIHN1bSBsZWZ0IG9zIDMgLSAxID0gMgogICAgICAgICAgICAvLyBHaXZlbiB3ZSBhcmUgY2FsY3VsYXRpbmcgZm9yIHN1bSAzIG1lYW5zIHdlIGhhdmUgYWxyZWFkeQogICAgICAgICAgICAvLyBjYWxjdWxhdGVkIGZvciBzdW0gMi4gU28gZ28gdG8gdGFibGUgZm9yIHN1bSA9IDIgYW5kCiAgICAgICAgICAgIC8vIGdldCB0aGUgbWluIG51bWJlciBvZiB3YXlzIHN1bSAyIGlzIGNvbXB1dGVkLgogICAgICAgICAgICAvLyBIZXJlIGkgaXMgdGhlIHN1bSBpLmUuIGxldHMgc2F5IGFzIHBlciBvdXIgZXhhbXBsZQogICAgICAgICAgICAvLyBpID0gMywgaiA9IDAKICAgICAgICAgICAgLy8gMSArIHRhYmxlWzMgLSBjaGFuZ2VbMF1dOwogICAgICAgICAgICAvLyAxICsgdGFibGVbMyAtIDFdOwogICAgICAgICAgICAvLyAxICsgdGFibGVbMl07CiAgICAgICAgICAgIC8vIDEgKyAyID0gMwogICAgICAgICAgICAvLyBTdW0gMyByZXF1aXJlcyBhdCBsZWFzdCAzIGNvaW5zIHsxLCAxLCAxfQogICAgICAgICAgICAKICAgICAgICAgICAgaWYgKG1pbiA+ICgxICsgdGFibGVbc3VtIC0gY2hhbmdlW2pdXSkpIHsKICAgICAgICAgICAgICAgIG1pbiA9IDEgKyB0YWJsZVtzdW0gLSBjaGFuZ2Vbal1dOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHRhYmxlW3N1bV0gPSBtaW47CiAgICAgICAgbWluID0gSU5UMzJfTUFYOwogICAgfQogICAgCiAgICBmb3IgKGkgPSAwOyBpIDw9IFNVTTsgaSsrKSB7CiAgICAgICAgcHJpbnRmKCJTVU1bJWRdOiAlZFxuIiwgaSwgdGFibGVbaV0pOwogICAgfQogICAgCiAgICByZXR1cm4gdGFibGVbU1VNXTsKfQoKCmludCBtYWluKGludCBhcmdjLCBjb25zdCBjaGFyICogYXJndltdKQp7CiAgICBpbnQgY2hhbmdlWzRdID0gezEsIDUsIDEwLCAyNX07CiAgICBpbnQgY2hhbmdlU2l6ZSA9IHNpemVvZihjaGFuZ2UpL3NpemVvZihjaGFuZ2VbMF0pOwogICAgaW50IFNVTSA9IDE2OwogICAgCiAgICBwcmludGYoIk1pbkNoYW5nZSBmb3Igc3VtICVkID0gJWRcbiIsIFNVTSwgbWluY291bnQoY2hhbmdlLCBjaGFuZ2VTaXplLCBTVU0pKTsKICAgIAogICAgcmV0dXJuIDA7Cn0=