Here is link of the question
I started with score of 10 then i stuck at 80 and 90 and finally i got score of 100. Here is my solution if you have better solution please share with us.
Download the below Code: Click Here
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
public class CrossingRiver {
private static int previousMax;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String input[] = {""};
System.out.println(bridge(input));
}
@SuppressWarnings("unchecked")
public static int bridge(String[] input1)
{
System.out.println(input1.length);
if(input1.length==0)
return 0;
int arr[];
arr=new int[2*input1.length];
StringTokenizer st;
for(int i=0,j=0;i {
st = new StringTokenizer(input1[i],"#");
while(st.hasMoreElements())
{
arr[j] = Integer.valueOf((String)st.nextElement());
j++;
}
}
for(int j:arr)
System.out.println(j);
Set set = new HashSet();
for(int j=0;j {
boolean b = set.add(arr[j]);
if(b==false)
return -1;
}
set.clear();
for(int j=1;j {
boolean b = set.add(arr[j]);
if(b==false)
return -1;
}
if(input1.length==1)
{
if(input1[0].equals(""))
return 0;
return 1;
}
else
{
int cityFirst;
int citySecond;
int cityFirstInside;
int citySecondInside;
int start;
int end;
int previous1;
boolean flag=false;
int max=0;
ArrayList al = new ArrayList();
for(int i=0;i {
cityFirst = arr[i];
citySecond = arr[i+1];
previous1 = citySecond;
for(int j=0;j {
cityFirstInside = arr[j];
citySecondInside = arr[j+1];
//1#2,2#3,3#1,4#4,5#7,6#5,7#6
if(cityFirst==cityFirstInside && citySecond==citySecondInside)
{
System.out.println("--------------------"+cityFirst+"----"+cityFirstInside+"------------");
max++;
}
else if(cityFirstInside>cityFirst)
{
if(previous1 {
max++;
System.out.println("happy"+cityFirstInside);
previous1=citySecondInside;
}
}
else if(cityFirstInside {
System.out.println("C1="+cityFirstInside+" C2="+citySecondInside+" max="+max);
if(j==0)
{
max++;
al.add(citySecondInside);
}
else
{
for(int p=0;p {
if(!(citySecondInside>(Integer)al.get(p)))
{
System.out.println("hhh");
flag=true;
break;
}
}
System.out.println(flag);
if(!flag)
{
max++;
al.add(citySecondInside);
// System.out.println("C1="+cityFirstInside+" C2="+citySecondInside+" max="+max);
}
flag=false;
}
}
}
al.clear();
if(max>previousMax)
previousMax = max;
max=0;
}
}
return previousMax;
}
Download the above Code: Click Here

0 comments:
Post a Comment