raghuram2404
BAN USER- -1of 3 votes
Answerswrite a sample code to find no of 'a' words in a sentence?
- raghuram2404 in India
Eg: If a sentence is given as "I found an apple in a tree."
The output is : 1 (not 2)
We have to count no of words.| Report Duplicate | Flag | PURGE
Microsoft Software Engineer in Test Algorithm
public class TwoThread {
int startingNumber = 1;
int endingNumber = 20;
int printingNumber = 1;
Thread thread1 = null, thread2 = null;
public TwoThread(){
thread1 = new Thread1();
thread2 = new Thread2();
thread1.start();
thread2.start();
thread2.suspend();
}
public void end(){
if(thread1.isAlive())
thread1.stop();
if(thread2.isAlive())
thread2.destroy();
}
public class Thread1 extends Thread{
// odd number printing thread
@SuppressWarnings("deprecation")
public void run(){
while(printingNumber <= endingNumber){
if(printingNumber%2 != 0){
System.out.print(printingNumber+++" ");
thread2.resume();
thread1.suspend();
}
}
if(endingNumber%2 == 0){ // let the other thread finish it's execution
thread2.resume();
}
}
}
public class Thread2 extends Thread{
// even number printing thread
public void run(){
while(printingNumber <= endingNumber){
if(printingNumber%2 == 0){
System.out.print(printingNumber+++" ");
thread1.resume();
thread2.suspend();
}
}
if(endingNumber%2 != 0){ // let the other thread finish it's execution
thread1.resume();
}
}
}
public static void main(String args[]){
TwoThread tt = new TwoThread();
}
}
I guess my algorithm may save some time in solving
- raghuram2404 June 07, 2013Algorithm:
1. Construct the graph with all peaks as nodes and there will be an edge between each adjacent node which is directed from higher to lower peak
2.Get the nodes which has inbound as '0' because that is the starting point that a longest path can start from.
3.Get the nodes which has outbound as '0' because that is the end point that a longest path can end.
4.Now we have only few nodes to consider.
5.Do dfs for all nodes with 0 inbound (of course each dfs will end in 0 outbound vertex).
6.Now the dfs with maximum length will be the solution
My solution will decrease the usage of dfs in the algorithm.