Finding Minimum travel time to Nearest city from Current location
Last Updated :
04 May, 2023
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;
int minimumTime( int N, int cur, int pos[], int time [])
{
int mn = numeric_limits< int >::max();
for ( int i = 0; i < N; i++) {
int dist = abs (pos[i] - cur);
mn = min(mn, dist * time [i]);
}
return mn;
}
int main()
{
int N = 3;
int cur = 4;
int pos[] = { 1, 5, 6 };
int time [] = { 2, 3, 1 };
int minTime = minimumTime(N, cur, pos, time );
cout << minTime << endl;
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static int minimumTime( int N, int cur, int [] pos,
int [] time)
{
int mn = ( int )1e9;
for ( int i = 0 ; i < N; i++) {
int dist = Math.abs(pos[i] - cur);
mn = Math.min(mn, dist * time[i]);
}
return mn;
}
public static void main(String[] args)
{
int N = 3 ;
int cur = 4 ;
int [] pos = { 1 , 5 , 6 };
int [] time = { 2 , 3 , 1 };
int minTime = minimumTime(N, cur, pos, time);
System.out.println(minTime);
}
}
|
C#
using System;
public class GFG
{
public static int minimumTime( int N, int cur, int [] pos, int [] time)
{
int mn = ( int )1e9;
for ( int i = 0; i < N; i++)
{
int dist = Math.Abs(pos[i] - cur);
mn = Math.Min(mn, dist * time[i]);
}
return mn;
}
public static void Main( string [] args)
{
int N = 3;
int cur = 4;
int [] pos = { 1, 5, 6 };
int [] time = { 2, 3, 1 };
int minTime = minimumTime(N, cur, pos, time);
Console.WriteLine(minTime);
}
}
|
Python3
import sys
def minimumTime(N, cur, pos, time):
mn = sys.maxsize
for i in range (N):
dist = abs (pos[i] - cur)
mn = min (mn, dist * time[i])
return mn
if __name__ = = "__main__" :
N = 3
cur = 4
pos = [ 1 , 5 , 6 ]
time = [ 2 , 3 , 1 ]
minTime = minimumTime(N, cur, pos, time)
print (minTime)
|
Javascript
function minimumTime(N, cur, pos, time) {
let mn = Number.MAX_SAFE_INTEGER;
for (let i = 0; i < N; i++) {
let dist = Math.abs(pos[i] - cur);
mn = Math.min(mn, dist * time[i]);
}
return mn;
}
const N = 3;
const cur = 4;
const pos = [1, 5, 6];
const time = [2, 3, 1];
const minTime = minimumTime(N, cur, pos, time);
console.log(minTime);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...