Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Diagnostics;
- namespace Z5_BuyGraphs
- {
- class Program
- {
- // Dynamic programming, bottoms-up approach
- // Ref: [http://www.ccs.neu.edu/home/jaa/CSG713.04F/Information/Handouts/dyn_prog.pdf]
- public static int[] denoms = { 1, 8, 9, 27, 28, 31, 36 };
- public static int[] BigC;
- public static int[] BigS;
- static void Main(string[] args)
- {
- int money = 46;
- Console.WriteLine(CoinCounting(money));
- while (money > 0)
- {
- Console.Write(denoms[BigS[money]] + ", ");
- money -= denoms[BigS[money]];
- }
- }
- // O(NlogN + NK) //Sort + 2 loops
- private static int CoinCounting(int changeThis)
- {
- int coinsNeededMin = 0;
- int firstCoin = 0;
- BigC = new int[changeThis + 1];
- BigS = new int[changeThis + 1];
- Array.Sort(denoms);
- for (int i = 1; i <= changeThis; i++)
- {
- coinsNeededMin = int.MaxValue;
- for (int j = 0; j < denoms.Length && denoms[j] <= i; j++)
- {
- int result = 1 + BigC[i - denoms[j]];
- if (result < coinsNeededMin)
- {
- coinsNeededMin = result;
- firstCoin = j;
- }
- }
- BigC[i] = coinsNeededMin;
- BigS[i] = firstCoin;
- }
- return coinsNeededMin;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement