Answer to Question #216723 in Java | JSP | JSF for Anonymous

Question #216723

Rahul lives in City A and would like to travel to City B for work. There is a shuttle service for people in these cities which has a fixed schedule. The schedule for City A is a list of boarding times(at City A) and departure times(from City A) for each bus.

Note: No one is allowed to board a shuttle after the boarding time.

He arrives at time t and sometimes has to wait at the station. You are given a list of arrival times for n days.

  1. Devise an algorithm to find him the shuttle with the least waiting time. (waiting time  = boardingj - t ,  where j is the next shuttle. And boardingj  >= t ) for each ti
  2. If he also has access to the travel time of each shuttle. Can you help him find the shuttle which will help him reach his destination at the earliest ?

Return an array of shuttle indexes that Rahul took for n days

If there is no such shuttle return -1.


Write your algorithms as pseudo-code and make sure to explain how your algorithm works and the time and space complexity of your algorithm.




1
Expert's answer
2021-07-13T13:39:12-0400
import java.io.*;
 
class Main {
    public static int get(int arr[], int dep[],
                                   int n)
    {
        int p= 1, rlt = 1;
        int i = 1, j = 0;
        for (i = 0; i < n; i++) {
            p = 1;
 
            for (j = i + 1; j < n; j++) {
                if ((arr[i] >= arr[j] && arr[i] <= dep[j])
                    || (arr[j] >= arr[i]
                        && arr[j] <= dep[i]))
                    p++;
            }
            rlt = Math.max(rlt, p);
        }
 
        return rlt;
    }
    public static void main(String[] args)
    {
        int arr[] = {1800, 950, 940, 900, 1500,1800 };
        int dep[] = { 1200, 1120,910,2000,1900,2000 };
        int n = 6;
        System.out.println(get(arr, dep, n));
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS