Friday, January 10, 2025
Google search engine
HomeData Modelling & AIFinding Minimum travel time to Nearest city from Current location

Finding Minimum travel time to Nearest city from Current location

Given an integer N representing the number of cities near you along with two arrays pos[] representing the position of the city and time[] representing the time required to move 1 unit of distance for a particular city, the task is to find the minimum time required to visit any city where you are currently at cur position.

Examples:

Input: N = 3, cur = 4, pos[] = [1, 5, 6], time[] = [2, 3, 1]
Output: 2
Explanation:

  • Total time taken by the 1st taxi will be: (4-1)*2 = 6
  • Total time taken by the 2nd taxi will be: (5-4)*3 = 3
  • Total time taken by the 3rd taxi will be: (6-4)*1 = 2

So, the minimum time will be 2 sec.

Input: N = 2, cur = 1, pos[] = [1, 6], time[] = [10, 3]
Output: 0
Explanation:

  • Total time taken by the 1st taxi will be: (1-1)*10 = 0
  • Total time taken by the 2nd taxi will be: (6-1)*3 = 15

So, the minimum time will be 0 sec.

Approach: This can be solved with the following idea:

Determine the lowest amount of time required by you to reach each city by first calculating the absolute distance between you and each city.

Below are the steps involved in the implementation of the code:

  • Run a loop from 0 to N(number of taxis).
  • Keep calculating the distance and then multiply it by the time taken by you to travel each city per unit distance.
  • Now keep storing the minimum time.
  • Return in the min time possible.

Below is the code implementation of the above code:

C++




#include <cmath>
#include <iostream>
#include <limits>
 
using namespace std;
 
// Minimum time required to
// reach any city
int minimumTime(int N, int cur, int pos[], int time[])
{
    // Initialising with maximum
    // distance
    int mn = numeric_limits<int>::max();
 
    // iterate over all positions
    for (int i = 0; i < N; i++) {
 
        // Calculate the distance from
        // the current position to the
        // current position in pos
        int dist = abs(pos[i] - cur);
 
        // Update mn if the time
        // taken to move to the
        // current position is
        // smaller than the
        // previous minimum time
        mn = min(mn, dist * time[i]);
    }
 
    // Return the minimum time taken
    // to move to any position
    return mn;
}
 
// Driver code
int main()
{
    int N = 3;
    int cur = 4;
    int pos[] = { 1, 5, 6 };
    int time[] = { 2, 3, 1 };
 
    // Function call
    int minTime = minimumTime(N, cur, pos, time);
    cout << minTime << endl;
    return 0;
}
 
// This code is contributed by Ayush pathak


Java




import java.util.*;
public class GFG {
 
    // Minimum time required to
    // reach any city
    public static int minimumTime(int N, int cur, int[] pos,
                                  int[] time)
    {
 
        // Initialising with maximum
        // distance
        int mn = (int)1e9;
 
        // iterate over all positions
        for (int i = 0; i < N; i++) {
 
            // Calculate the distance from
            // the current position to the
            // current position in pos
            int dist = Math.abs(pos[i] - cur);
 
            // Update mn if the time
            // taken to move to the
            // current position is
            // smaller than the
            // previous minimum time
            mn = Math.min(mn, dist * time[i]);
        }
 
        // Return the minimum time taken
        // to move to any position
        return mn;
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        int N = 3;
        int cur = 4;
        int[] pos = { 1, 5, 6 };
        int[] time = { 2, 3, 1 };
 
        // Function call
        int minTime = minimumTime(N, cur, pos, time);
        System.out.println(minTime);
    }
}


C#




using System;
 
public class GFG
{
    // Minimum time required to
    // reach any city
    public static int minimumTime(int N, int cur, int[] pos, int[] time)
    {
        // Initialising with maximum
        // distance
        int mn = (int)1e9;
 
        // iterate over all positions
        for (int i = 0; i < N; i++)
        {
            // Calculate the distance from
            // the current position to the
            // current position in pos
            int dist = Math.Abs(pos[i] - cur);
 
            // Update mn if the time
            // taken to move to the
            // current position is
            // smaller than the
            // previous minimum time
            mn = Math.Min(mn, dist * time[i]);
        }
 
        // Return the minimum time taken
        // to move to any position
        return mn;
    }
 
    // Driver code
    public static void Main(string[] args)
    {
        int N = 3;
        int cur = 4;
        int[] pos = { 1, 5, 6 };
        int[] time = { 2, 3, 1 };
 
        // Function call
        int minTime = minimumTime(N, cur, pos, time);
        Console.WriteLine(minTime);
    }
}


Python3




import sys
 
# Minimum time required to
# reach any city
 
 
def minimumTime(N, cur, pos, time):
    # Initialising with maximum
    # distance
    mn = sys.maxsize
 
    # iterate over all positions
    for i in range(N):
 
        # Calculate the distance from
        # the current position to the
        # current position in pos
        dist = abs(pos[i] - cur)
 
        # Update mn if the time
        # taken to move to the
        # current position is
        # smaller than the
        # previous minimum time
        mn = min(mn, dist * time[i])
 
    # Return the minimum time taken
    # to move to any position
    return mn
 
 
# Driver code
if __name__ == "__main__":
    N = 3
    cur = 4
    pos = [1, 5, 6]
    time = [2, 3, 1]
 
    # Function call
    minTime = minimumTime(N, cur, pos, time)
    print(minTime)


Javascript




function minimumTime(N, cur, pos, time) {
  // Initialising with maximum distance
  let mn = Number.MAX_SAFE_INTEGER;
 
  // iterate over all positions
  for (let i = 0; i < N; i++) {
    // Calculate the distance from
    // the current position to the
    // current position in pos
    let dist = Math.abs(pos[i] - cur);
 
    // Update mn if the time
    // taken to move to the
    // current position is
    // smaller than the
    // previous minimum time
    mn = Math.min(mn, dist * time[i]);
  }
 
  // Return the minimum time taken
  // to move to any position
  return mn;
}
 
// Driver code
const N = 3;
const cur = 4;
const pos = [1, 5, 6];
const time = [2, 3, 1];
 
// Function call
const minTime = minimumTime(N, cur, pos, time);
console.log(minTime);


Output

2

Time Complexity: O(N)
Auxiliary Space: O(1) 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Commit to GfG’s Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated :
04 May, 2023
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Share your thoughts in the comments

RELATED ARTICLES

Most Popular

Recent Comments