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); }
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
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; }
Given a Binary Tree, find vertical sum of the nodes that are in same vertical line. Print all sums through different vertical lines. Examples: 1 / \ 2 3 / \ / \ 4 5 6 7 The tree has 5 vertical lines Vertical-Line-1 has only one node 4 => vertical sum is 4 Vertical-Line-2: has only one node 2=> vertical sum is 2 Vertical-Line-3: has three nodes: 1,5,6 => vertical sum is 1+5+6 = 12 Vertical-Line-4: has only one node 3 => vertical sum is 3 Vertical-Line-5: has only one node 7 => vertical sum is 7 So expected output is 4, 2, 12, 3 and 7
int coin_count(int s, int m)
ReplyDelete{
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);
}
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).
ReplyDeleteFor 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
We can also use backtracking.
ReplyDelete//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