给定一个大小为N的数组arr[] ,其中arr[i]是访问第i个城市所需的时间,任务是找到两个人访问所有N个城市所需的最短总时间,使得他们在任何一个城市。
例子:
Input: arr[] = {2, 8, 3}
Output: 16
Explanation:
Visiting cities in below given order will take minimum time:
First person: 2nd city → 1st city → 3rd city
Second person: 1st city → 3rd city → 2nd city.
Input: arr[]={1, 10, 6, 7, 5}
Output: 29
方法:根据以下观察可以解决给定的问题:
- 假设第i个城市的访问时间最长为T ,一个人访问所有城市的总时间是所有数组元素的总和,比如sum 。
- 如果第一人参观第i个城市,那么在时间T,第二个人将参观在那个时候其他城市,如果可能的话。
- 如果值T至多(sum – T) ,则两个人可以在sum时间内单独访问该地点。
- 否则,第二人将不得不等待参观的第i个城市。那么,所需的总时间将是2 * T,因为只有第一个人出来,第二个人才能访问第i个城市。
- 因此,根据上述观察,答案将是2 * T和sum 的最大值。
因此,根据上述观察,找到数组元素的总和 /a> 并找到数组中存在的最大元素,并打印最大元素的两倍和总和中的最大值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum time
// to visit all the cities such that
// both the person never meets
void minimumTime(int* arr, int n)
{
// Initialize sum as 0
int sum = 0;
// Find the maximum element
int T = *max_element(arr, arr + n);
// Traverse the array
for (int i = 0; i < n; i++) {
// Increment sum by arr[i]
sum += arr[i];
}
// Print maximum of 2*T and sum
cout << max(2 * T, sum);
}
// Driver Code
int main()
{
int arr[] = { 2, 8, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
minimumTime(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to find the minimum time
// to visit all the cities such that
// both the person never meets
static void minimumTime(int[] arr, int n)
{
// Initialize sum as 0
int sum = 0;
// Find the maximum element
int T = Arrays.stream(arr).max().getAsInt();
// Traverse the array
for (int i = 0; i < n; i++)
{
// Increment sum by arr[i]
sum += arr[i];
}
// Print maximum of 2*T and sum
System.out.println(Math.max(2 * T, sum));
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 2, 8, 3 };
int N = arr.length;
// Function Call
minimumTime(arr, N);
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program for the above approach
# Function to find the minimum time
# to visit all the cities such that
# both the person never meets
def minimumTime(arr, n):
# Initialize sum as 0
sum = 0
# Find the maximum element
T = max(arr)
# Traverse the array
for i in range(n):
# Increment sum by arr[i]
sum += arr[i]
# Prmaximum of 2*T and sum
print(max(2 * T, sum))
# Driver Code
if __name__ == '__main__':
arr = [2, 8, 3]
N = len(arr)
# Function Call
minimumTime(arr, N)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Linq;
class GFG
{
// Function to find the minimum time
// to visit all the cities such that
// both the person never meets
static void minimumTime(int[] arr, int n)
{
// Initialize sum as 0
int sum = 0;
// Find the maximum element
int T = arr.Min();
// Traverse the array
for (int i = 0; i < n; i++)
{
// Increment sum by arr[i]
sum += arr[i];
}
// Print maximum of 2*T and sum
Console.WriteLine(Math.Max(2 * T, sum));
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 2, 8, 3 };
int N = arr.Length;
// Function Call
minimumTime(arr, N);
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
16
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live