From 5732efb4a4a95692faebbba554b6fe22fa18fd5e Mon Sep 17 00:00:00 2001 From: Aman Sharma Date: Wed, 17 Oct 2018 16:20:55 +0530 Subject: [PATCH] Coin Change solution --- coin-change.cs | 57 +++++++++++++++++++++++++++++++++++++++++++++ string-reduction.cs | 50 --------------------------------------- 2 files changed, 57 insertions(+), 50 deletions(-) create mode 100644 coin-change.cs delete mode 100644 string-reduction.cs diff --git a/coin-change.cs b/coin-change.cs new file mode 100644 index 00000000..708ad28d --- /dev/null +++ b/coin-change.cs @@ -0,0 +1,57 @@ +using System; +using System.Linq; +using System.Collections.Generic; +using System.Text; +using System.Numerics; +using System.Diagnostics; + +namespace ConsoleApplication +{ + + class Program + { + public static long[,] dp = new long[52, 252]; + + public static void Main(String[] args) + { + int[] l0 = Array.ConvertAll(Console.ReadLine().Trim().Split(' '), e => int.Parse(e)); + int[] coins = Array.ConvertAll(Console.ReadLine().Trim().Split(' '), e => int.Parse(e)); + int sum = l0[0]; + int n = l0[1]; + for(int i = 0; i < 52; i++) + { + for(int j = 0; j < 252; j++) + { + dp[i, j] = -1; + } + } + + Console.WriteLine(calculate(coins, sum, n - 1, 0)); + } + + public static long calculate(int[] coins, int sum, int numCoin, int i) + { + if(sum == 0) + { + return 1; + } + + if(sum < 0) + { + return 0; + } + + if(i > numCoin) + { + return 0; + } + + if(dp[i, sum] == -1) + { + dp[i, sum] = calculate(coins, sum, numCoin, i + 1) + calculate(coins, sum - coins[i], numCoin, i); + } + + return dp[i, sum]; + } + } +} \ No newline at end of file diff --git a/string-reduction.cs b/string-reduction.cs deleted file mode 100644 index 671d8477..00000000 --- a/string-reduction.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -class Solution { - - static int stringReduction (string a) { - int[] ar = new int[3]; - for(int i = 0; i < a.Length; i++) - { - if(a[i] == 'a') - { - ar[0]++; - }else if(a[i] == 'b') - { - ar[1]++; - } - else - { - ar[2]++; - } - } - - if((ar[0] == 0 && ar[1] == 0) || (ar[1]== 0 && ar[2] == 0)||(ar[0] == 0 && ar[2] == 0)) - { - return a.Length; - } - - if ((ar[0] % 2 == 0 && ar[1] %2== 0) && (ar[1] %2 == 0 && ar[2] %2== 0)) - { - return 2; - } - - if ((ar[0] % 2 == 1 && ar[1] % 2 == 1) && (ar[1] % 2 == 1 && ar[2] % 2 == 1)) - { - return 2; - } - - return 1; - } - - static void Main(String[] args) { - int res; - int _t_cases = Convert.ToInt32(Console.ReadLine()); - for (int _t_i=0; _t_i<_t_cases; _t_i++) { - String _a = Console.ReadLine(); - res=stringReduction(_a); - Console.WriteLine(res); - } - } -}