Avinash Kumar
BAN USER1. Take max marks from each subject with studentName
2. create one map <StudentName, count of subject having max mark)
3. now iterate over max marks and increase the count of subject having max marks in map.
4. iterate over map and find all those having count >=n.
Use spiral level order traversal technique.
Take two sum varialbe oddSum and evenSum and two stacks s1 and s2.
Take one boolean variable evenLevel.
java code is below:
public int maxAmountRob(BTNode<T> node) {
if (node == null) {
return;
}
Stack<BTNode<T>> stack1 = new Stack<>();
Stack<BTNode<T>> stack2 = new Stack<>();
stack2.push(node);
boolean levelFlag = false;
int oddSum=0;
int evenSum=0;
while (!(stack1.isEmpty() && stack2.isEmpty())) {
BTNode<T> currentNode;
if (levelFlag) {
while (!stack1.isEmpty()) {
currentNode= stack1.pop();
oddSum+=currentNode.getData();
if(currentNode.getLeft() != null){
stack2.push(currentNode.getLeft());
}
if(currentNode.getRight() != null){
stack2.push(currentNode.getRight());
}
}
levelFlag = false;
} else {
while (!stack2.isEmpty()) {
currentNode= stack2.pop();
evenSum+=currentNode.getData();
if(currentNode.getRight() != null){
stack1.push(currentNode.getRight());
}
if(currentNode.getLeft() != null){
stack1.push(currentNode.getLeft());
}
}
levelFlag = true;
}
}
return Math.max(oddSum,evenSum);
}
- Avinash Kumar December 14, 2016Here is the code in java:
public int maxOnesInMatrix(int[][]A, int row, int col){
int count =0;
if(A[0][0]==1){
count = col;
}
else{
for(int i=0; i< row; i++){
if(count==col-1){
break;
}
int oldCount =count;
for(int j= col-oldCount-1; j >=0; j--){
if(A[i][j]==0){
break;
}
count++;
}
}
}
return count;
}
It is simple if we make father childList Map.
- Avinash Kumar December 19, 20161. Create Map<Father, List<Child>> by iterating over pairs.
2. For level =1 map.get(father) which in turn returns childList (List<Child>) totalChildren = childList.size()
3. For level = 2 , Iterate over childList and each element of childList is now father so map.get(childList(i)) and add the size of return value for totalChildren.
repeat same for the next level.