给定大小为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) ,那么两个人都可以在总和时间内分别访问该地点。
- 否则,第二个人将不得不等待参观第一个城市。然后,所需的总时间将为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)