Reputation: 12201
There are equal number of cities on each side of the river. A bridge is there from city on one side to the city on other side represented by 1#y3
where city 1 on the lower side having bridge to the city 3 on upper side. We have to find maximum number of non overlapping bridges. So for input 1#y2, 2#y4, 3#y1, 4#y5, 5#y3, 6#y6
out will be 4
as 1#y2, 2#y4, 4#y5, 6#y6
are non overlapping.
This is my code -
public static int maxNonOverlappingBridges(String input1[]) {
int result = 0;
for (int i = 0; i < input1.length; i++) {
int total = 1;
int notCrossing = Integer.parseInt(input1[i].substring(input1[i].length() - 1));
for (int j = 0; j < input1.length; j++) {
if (j < i) {
if (Integer.parseInt(input1[j].substring(input1[j].length() - 1)) < notCrossing) {
total += 1;
notCrossing = Integer.parseInt(input1[j].substring(input1[j].length() - 1));
}
} else if (j > i) {
if (Integer.parseInt(input1[j].substring(input1[j].length() - 1)) > notCrossing) {
total += 1;
notCrossing = Integer.parseInt(input1[j].substring(input1[j].length() - 1));
}
} else {
notCrossing = Integer.parseInt(input1[j].substring(input1[j].length() - 1));
}
}
if (total > result) result = total;
}
return result;
}
Is there a more optimized algorithm for this?
Upvotes: 1
Views: 292
Reputation: 312
simple solution with O(nLog(n)) ...
public static int bridge_count1(String[] input1,int input2)
{
int count=0;
Map<Integer,Integer> map=new TreeMap<Integer,Integer>();
for(int i=0;i<input1.length;i++)
{
String cityCon=input1[i];
String cityArray[]=cityCon.split("#");
int fst=Integer.parseInt(cityArray[0]);
int sec=Integer.parseInt(cityArray[1]);
map.put(fst, sec);
}
Iterator<Integer> it=map.keySet().iterator();
int max=0;
while(it.hasNext())
{
int val=map.get(it.next());
if(max<=val)
{
max=val;
count++;
}
}
return count;
}
Upvotes: 1
Reputation: 9291
This runs in O(nLog(n)).
public static int maxNonOverlappingBridges(String[] bridges) {
int overlappings1 = 0;
int overlappings2 = 0;
int maxIndexOfCityOnOtherSide = 0;
/*
* Sorting bridge connections with respect to the left index. (i.e : 2 in 2#y4)
* O(n*log(n)) time
*/
Arrays.sort(bridges);
List<Integer[]> brigesAsArray = new ArrayList<>();
Integer[] previousBridge = null;
for(String b: bridges){
if(previousBridge == null){
String[] bridgeIndexes = b.split("#y");
previousBridge = new Integer[]{Integer.parseInt(bridgeIndexes[0]),Integer.parseInt(bridgeIndexes[1])};
maxIndexOfCityOnOtherSide = previousBridge[1];
brigesAsArray.add(previousBridge);
continue;
}
String[] bridgeIndexes = b.split("#y");
Integer[] currentBridge = new Integer[]{Integer.parseInt(bridgeIndexes[0]),Integer.parseInt(bridgeIndexes[1])};
if(currentBridge[1] < maxIndexOfCityOnOtherSide){
//overlapping found;
overlappings1++;
}else{
maxIndexOfCityOnOtherSide = currentBridge[1];
}
previousBridge = currentBridge;
brigesAsArray.add(previousBridge);
}
Integer[][] bridgeIndexes = brigesAsArray.toArray(new Integer[0][0]);
brigesAsArray.toArray(bridgeIndexes);
/*
* Sorting bridge connections with respect to the right index. (i.e : 4 in 2#y4)
* O(n*log(n)) time
*/
Arrays.sort(bridgeIndexes, new Comparator<Integer[]>() {
@Override
public int compare(Integer[] o1, Integer[] o2) {
return Integer.compare(o1[1], o2[1]);
}
});
previousBridge = null;
for(Integer[] b: bridgeIndexes){
if(previousBridge == null){
maxIndexOfCityOnOtherSide = b[0];
previousBridge = b;
continue;
}
if(b[0] < maxIndexOfCityOnOtherSide){
//overlapping found;
overlappings2++;
}else{
maxIndexOfCityOnOtherSide = b[0];
}
previousBridge = b;
brigesAsArray.add(previousBridge);
}
return (bridges.length - Math.min(overlappings1, overlappings2));
}
Hope this helps.
Upvotes: 1
Reputation: 312
public static int bridge_count(String[] input1,int input2)
{
int[][] track=new int[input2][input2];
int count=0;
for(int i=0;i<input1.length;i++)
{
String cityCon=input1[i];
String cityArray[]=cityCon.split("#");
int fst=Integer.parseInt(cityArray[0]);
int sec=Integer.parseInt(cityArray[1]);
track[fst-1][sec-1]=-1;
}
int found=0;
int maxCol=-1;
for(int c=0;c<input2;c++)
{
found=-1;
for(int j=0;j<input2;j++)
{
if(track[c][j]==-1)
{
found=j;
break;
}
}
if(found!=-1 && maxCol<=found)
{
count++;
maxCol=found;
}
}
return count;
}
Upvotes: 0