rahul.singh4899
BAN USERThats what happened to me initially. missed to think about line break, otherwise this is pure BFS. thanks, i will add null identifier.
- rahul.singh4899 April 22, 2015seeing the problem , i am not sure whether we have to find only one celebrity or there exist more than one. but in any case, the simpler means is the true value return for knows(i,i) ...as celebrity only has self loop. i am thinking interviewer was not iterated in knowing data structure skill but simple common sense or how well we try understanding the question.
When i see the problem i thought this as a graph and with the assumption that there is only one celebrity , in that case the graph Vertex whose adjust size is n-1 is the celebrity.
this solution will not work if repletion is allowed . ex: as provided in the problem,
{10,-2,1}
what was the change, not sure why u were checking j==k or i==k
- rahul.singh4899 April 20, 2015does ordering of the data matters .
my code break if i add the hierarchy after finishing the root node
import java.util.*;
class reportManager {
public static void main(String []args) {
Map<String,String> inPut = new HashMap<String,String>();
inPut.put("A","C");
inPut.put("B","C");
inPut.put("C","F");
inPut.put("D","E");
inPut.put("E","F");
inPut.put("G","F");
inPut.put("F","F");
inPut.put("M","G");
Map<String,Integer> outPut= new HashMap<String,Integer>();
Iterator<Map.Entry<String,String>> entries= inPut.entrySet().iterator();
while(entries.hasNext()){
Map.Entry<String,String> entry= entries.next();
if(!(outPut.containsKey(entry.getKey()))){
if(!(outPut.containsKey(entry.getValue()))){
outPut.put(entry.getKey(),0);
outPut.put(entry.getValue(),1);
}
else {
outPut.put(entry.getValue(),(outPut.get(entry.getValue())+1));
outPut.put(entry.getKey(),0);
}
}
else {
if(!(outPut.containsKey(entry.getValue()))){
outPut.put(entry.getValue(),outPut.get(entry.getKey())+1);
}
else {
if(!(entry.getKey().equals(entry.getValue())))
outPut.put(entry.getValue(),(outPut.get(entry.getValue()) + (outPut.get(entry.getKey()))+1));
}
}
}
//printing the value of the putPut String
for(Map.Entry<String,Integer> entry1 :outPut.entrySet()){
System.out.println(""+entry1.getKey()+":"+entry1.getValue());
}
}
}
below is my solution which works for both positive and negative number .rather adding to achieve the SUM ,my approach is to subtract and achieve 0, I have outer while loop on the list of array which will be incremented when we see negative number,value greater that SUM expected or the SUM computed greater than SUM expected.
- rahul.singh4899 April 26, 2015