[DP]Coin Counting Problem

Given 1, 2, 5, and 10 paise coins, how many ways can you make a rupee?
Hint: Order doesn't matter.

Comments

  1. int coin_count(int s, int m)
    {
    if (s == 0)
    return 1;
    if (s < 0)
    return 0;
    if (m < 0 && s >=1)
    return 0;
    return coin_count(s, m-1) + coin_count(s - num[m], m);
    }

    ReplyDelete
  2. Since order does not matter, we will impose that our solutions (sets) are all sorted in non-decreasing order (Thus, we are looking at sorted-set solutions: collections).

    For a particular N and S = \{ S_1, S_2, \ldots, S_m \} (now with the restriction that S_1 < S_2 < \ldots < S_m, our solutions can be constructed in non-decreasing order), the set of solutions for this problem, C(N,m), can be partitioned into two sets:

    There are those sets that does not contain any Sm,
    Those sets that contain at least 1 Sm,

    If a solution does not contain Sm, then we can solve the subproblem of N with S = \{ S_1, S_2, \ldots, S_{m-1} \}, or the solutions of C(N,m - 1).

    If a solution does contain Sm, then we are using at least one Sm, thus we are now solving the subproblem of N - Sm, S = \{ S_1, S_2, \ldots, S_m \}. This is C(N - Sm,m).

    Thus, we can formulate the following:

    C(N,m) = C(N,m - 1) + C(N - Sm,m)

    with the base cases:

    C(N,m) = 1,N = 0
    C(N,m) = 0,N < 0
    C( N, m ) = 0, N \geq 1, m \leq 0

    ReplyDelete
  3. We can also use backtracking.
    //Assume that input array is sorted in non-increasing order.
    Please check it's complexity.I am not sure.
    int subsetSum(int arr[],int len,int value,int i )
    {
    int tmp=0;
    if( i >=len) return 0;
    if( value== arr[i] )
    {
    cout<<" "< arr[i]){
    tmp=subsetSum(arr,len,value-arr[i], i );
    if(tmp)
    cout<<" "<<arr[i];
    else
    tmp=subsetSum(arr,len,value, i +1);
    }
    return tmp;
    }

    http://codepad.org/iHVDNvHr

    ReplyDelete

Post a Comment

Popular posts from this blog