Minimum platform required program

Minimum platform required  program
(Dynamic Programming :)
-------------------------------------------------------


The Minimum platform required  program of dynamic programming.This question has been collected from geeksforgeeks.com.



Here I tried to give a simple solution of this program by using map.The logic will be same as per explained in the screen shot below or in the site geeksforgeeks.com.







=========
CodE
=========




import java.util.*;

public class Main
{
public static void main(String[] args) {
Map<Double,String> m1=new TreeMap<Double,String>();
double arrival[]=new double[]{9.00,9.40,9.50,11.00,11.30,15.00,18.00};
double departure[]=new double[]{9.10,12.00,11.20,11.30,19.00,20.00};

//Adding the arrival time to a map named arrival
for(int i=0;i<arrival.length-1;i++)
{
    m1.put(arrival[i],"arrival");
}

                //Adding the departure time to a map named departure
for(int i=0;i<arrival.length-1;i++)
{
    m1.put(departure[i],"departure");
}
for(Map.Entry<Double,String> entry:m1.entrySet())
{
    System.out.println("key= "+ entry.getKey() +"Value= "+entry.getValue());
}

//Taking a variable count to 0 and an arrayList
    int count=0;
    List<Integer> al=new ArrayList<Integer>();
//take each entry from the map
// if the value is "Arrival"then add 1 to the count
//if the value is "departure" subtract 1 from count
for(Map.Entry<Double,String> entry:m1.entrySet())
{
    if(entry.getValue()=="arrival")
    {
    count ++;
    al.add(count);
    }
    else
    {
       count --;
       al.add(count);
    }
}


for(Integer i:al)
{
    System.out.println(i);
}


//Now acording to the login
//Maximum in the list will be the min number of platform required.
//so sort the array list
//print the last element

Collections.sort(al);
int n=al.get(al.size()-1);
System.out.println("The maximum platform required is :- ");
System.out.println(n);
}
}







#javaCoding
#javaProgramming
#MinimumPlatformRequiredProgram

Comments

Popular posts from this blog

Java Interview Preparation (conceptual)

Git Commands With Output Log

Java 8 Function Interface