-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathchange_dp.c
64 lines (55 loc) · 1.86 KB
/
change_dp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* ----------------------------------------------------------------------- *
*
* Author: Leandro Augusto Lacerda Campos <[email protected]>
*
* Data Structures and Algorithms Specialization,
* by University of California, San Diego,
* and National Research University Higher School of Economics
*
* Course 1: Algorithmic Toolbox
*
* Solution for Money Change Problem
*
* ----------------------------------------------------------------------- */
#include <stdlib.h>
#include <stdio.h>
#define MAX_MONEY 1000
#define MAX_NUM_COINS MAX_MONEY
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
static const uint8_t coins_denominations[] = {1, 3, 4}; /* in ascending
order */
static const uint8_t num_coins_denominations = 3;
static uint16_t change_table[MAX_MONEY + 1] = {0}; /* from 0 to MAX_MONEY */
static uint16_t change_table_last_index = 0;
uint16_t min_num_coins(uint16_t);
int main()
{
uint16_t money;
scanf("%hu", &money);
printf("%hu\n", min_num_coins(money));
return 0;
}
/*
* min_num_coins: finds the minimum number of coins that changes money.
*/
uint16_t min_num_coins(uint16_t money)
{
uint16_t i, num_coins;
if (money <= change_table_last_index)
return change_table[money];
while (++change_table_last_index <= money)
{
change_table[change_table_last_index] = MAX_NUM_COINS;
for (i = 0; i < num_coins_denominations; i++)
{
if (change_table_last_index >= coins_denominations[i])
{
num_coins = change_table[change_table_last_index - coins_denominations[i]] + 1;
if (num_coins < change_table[change_table_last_index])
change_table[change_table_last_index] = num_coins;
}
}
}
return change_table[money];
}