chengyuanzhang831
BAN USERThe complexity of a single operation, like contains(e) in HashSet, will be O(n) in the worst case, will this affect the complexity of the whole algorithm?
- chengyuanzhang831 January 07, 2014For the sentence "if(node.left==null){return node;}" I think the right subtree has not been transformed yet here, am I right?
- chengyuanzhang831 January 06, 2014public static void printLevels(TreeNode root){
List<TreeNode> la=new ArrayList<TreeNode>();
List<TreeNode> lb=new ArrayList<TreeNode>();
int level=0;
la.add(root);
while(true){
if(la.isEmpty()) break;
System.out.print("Level "+level+": ");level++;
for(TreeNode node:la){
System.out.print(node.getName()+" ");
lb.addAll(node.getChildren());
}
System.out.println();
la.clear();la.addAll(lb);
lb.clear();
}
}
I believe the TreeMap is used to sort the id, not the scores?
- chengyuanzhang831 January 06, 2014Trying to use recursive logic to get the total sales of one member's recruits
public static double calculateSales(Member member){
double sales=member.getMonthlySales();
Collection<Member> recruitedMembers=member.getRecruitedMembers();
for(Member mem:recruitedMembers){
sales+=calculateSales(mem);
}
return(sales);
}
public static double calculatePayout(Member member){
double sales=member.getMonthlySales();
double other=0;
Collection<Member> recruitedMembers=member.getRecruitedMembers();
for(Member mem:recruitedMembers){
other+=calculateSales(mem);
}
return(sales*0.1+other*0.04);
}
I tried to solve the problem by a DFS.
public class findWord {
static int n=4;
static int m=6;
static String[] matrix=new String[n];
static String word;
static int len;
static int ans=0;
static Step[] sa=new Step[100];
static Step step;
static void init(){
matrix[0]=new String("wsrtgg");
matrix[1]=new String("aachin");
matrix[2]=new String("kchujj");
matrix[3]=new String("ohinyq");
word=new String("sachin");
len=word.length();
System.out.println("The matrix is: ");
for(int i=0;i<n;i++) System.out.println(matrix[i]);
System.out.println("The word is: "+word);
}
static void dfs(int u,int v,int k){
if(matrix[u].charAt(v)!=word.charAt(k)) return;
step.add(v, u);
if(k==len-1){
sa[ans]=new Step(step);ans++;
step.remove();
return;
}
if(u+1<n) dfs(u+1,v,k+1);
if(v+1<m) dfs(u,v+1,k+1);
if(u+1<n&&v+1<m) dfs(u+1,v+1,k+1);
step.remove();
}
public static void main(String args[]){
init();
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
step=new Step();
dfs(i,j,0);
}
System.out.println("The number of same words is: "+ans);
for(int i=0;i<ans;i++) sa[i].out();
}
}
In these two sentences "int leftheight = height(node->left, balanced); int rightheight = height(node->right, balanced);" If the left subtree is not balanced, while the right one is balanced, will the variable "balanced" change to 1 again?
- chengyuanzhang831 January 05, 2014I figure out that there is no requirement for not creating new nodes. So I believe this solution should be OK.
- chengyuanzhang831 January 04, 2014Can this strategy guarantee that the elements in the heap is the biggest five ones?
- chengyuanzhang831 January 04, 2014
I tried to solve the equation.
- chengyuanzhang831 January 09, 2014And sum=m*m*m/6.0-(l-1)*m*m/2.0-(n*n/2.0-(l-1/2.0)*n+l/2.0-1/3.0)*m;