Sathya
BAN USER
for convenience i took denominations as boolean array
Split(int amount, bool[] denominations){
int[,] dp = new int[amount + 1, denominations.Length];
for (int i = 0; i < denominations.Length; i++)
dp[0, i] = 1;
for (int i = 1; i <= amount; i++)
for (int j = 1; j < denominations.Length; j++){
dp[i, j] = dp[i, j - 1];
if (denominations[j] && i>=j)
dp[i, j] += dp[i - j, j];
}
return dp[amount, denominations.Length-1];
}
@Algoseeker:yes duplicates can be either in left subtree or right.but for instance during insertion if left or right is decided based on a random number for a duplicate then it ll be lil more complicated to remove.but it can also be done with a small modification to the method i gave above
- Sathya March 07, 2011heres de code
class Node{
int data;
Node left;
Node right;
Node(int data){
this.data=data;
left=null;
right=null;
}
}
class BST{
Node head;
Node insert(Node node,int data){
if(node==null)
return new Node(data);
if(data<=node.data)
node.left=insert(node.left,data);
else
node.right=insert(node.right,data);
return node;
}
public static void main(String args[]){
new BST().go();
}
void go(){
int[] a=new int[]{80,150,70,80,70,70,140,110,110};//test case
for(int i:a)
head=insert(head,i);
inorder(head);
remove(head,-1);//I need an element which cant be in the bst for a neat recursion so i use -1
System.out.println();
inorder(head);
}
void inorder(Node n){
if(n==null)
return;
inorder(n.left);
System.out.print(n.data+" ");
inorder(n.right);
}
boolean remove(Node n,int d){
if(n==null)
return false;
if(remove(n.left,n.data))
n.left=n.left.left;
if(n.data==d)
return true;
if(remove(n.right,d))
n.right=n.right.left;
return false;
}
}
ok...my code is based on 1 assumption. A duplicate is always in the left subtree.I mean all values of n.left subtree is <=n.data...Now u can observe that a duplicate can have no right child also if an element 'e' is not a duplicate then the duplicates of all the elements above 'e' will fall in the right subtree of 'e'...try to imagine my algorithm based on these observations...I will give u a working code in java in my following post
- Sathya March 07, 2011class count{
int leaves;
int nonleaves;
}
count go(node n){
count c=new count();
if(n==null)
return c;
if(n.left==null && n.right==null){
c.leaves=1;
return c;
}
count cl=go(n.left);
count cr=go(n.right);
c.leaves=cl.leaves+cr.leaves;
c.nonleaves=cl.nonleaves+cr.nonleaves+1;
return c;
}
boolean isBalanced(Node n){
if(isBalancedHelp(n)==-1)
return false;
return true;
}
isBalancedHelp(Node n){
if(n is null)
return 0;
left=isBalancedHelp(n.left);
right=isBalancedHelp(n.right);
if(left==-1 || right==-1)
return -1;
if(left-right>=-1 && left-right<=1)
return 1+max(left,right);
return -1;
}
Heres the approach
if i and j are the 2 numbers...count the number of continuous ones at the front and back of i and j...let them ifrnt,iback,jfrnt,jback...
if(iback+jfrnt)>(jback+ifrnt)
i should come before j
else
j should come before i
eg:12315,123--->123 is 1111011--->front=4,back=2
12315 is 11000000011011--->front=2,back=2...so 12315 should come before 123
for(int start=0;start<st.length;start++){
prev=start;next=start
while(prev>=0 && next<st.length && st[prev]==st[next])
prev--;next++
if(found string > Max)
//save string
if(start<st.length-1)
if(st[start]==st[start+1]){
prev=start-1;next=start+2
while(prev>=0 && next<st.length && st[prev]==st[next])
prev--;next++
if(found string > Max)
//save string
}
}
if ur more concerned abt no: ifs in while loop use below
void go(){
int start=0,end=k-1,k;//k is input
start=findMax(start,end);
System.out.println(a[start]+" ");
end++;
while(end<a.length){
if(end-start==k){
start=findMax(start+1,end);
System.out.println(a[start]+" ");
end++;
}
else if(a[start]>a[end]){
System.out.println(a[start]+" ");
end++;
}
else{
System.out.println(a[end]+" ");
start=end;
end++;
}
}
}
this is the best i can come up with.
go(){
int start=0,end=k-1,k;//k is de input
while(end<a.length){
if(end-start==k-1){
start=findMax(start,end);
System.out.println(a[start]+" ");
end++;
}
else if(end-start==k)
start++;
else if(a[start]>a[end]){
System.out.println(a[start]+" ");
end++;
}
else{
System.out.println(a[end]+" ");
start=end;
end++;
}
}
}
int findMax(int start,int end){
int max=start++;
for(;start<=end;start++)
if(a[start]>a[max])
max=start;
return max;
}
- Sathya January 29, 2015