📌  相关文章
📜  尽量减少两个人访问N个城市所需的总时间,以使他们都不会见面

📅  最后修改于: 2021-05-05 01:41:11             🧑  作者: Mango

给定大小为N的数组arr [] ,其中arr [i]是访问i城市所需的时间,任务是找到两个人访问所有N个城市所需的最短总时间,以使他们中没有一个人会面任何城市。

例子:

方法:可以根据以下观察结果解决给定问题:

  • 假设i城市花费了最长的访问时间T ,一个人访问所有城市的总时间是所有数组元素的总和,例如sum
  • 如果第一人参观i城市,那么在时间T,第二个人将参观在那个时候其他城市,如果可能的话。
  • 如果值T最多为(sum – T) ,那么两个人都可以在总和时间内分别访问该地点。
  • 否则,第二个人将不得不等待参观第一个城市。然后,所需的总时间将为2 * T,因为只有第一个人出来时,第二个人才能访问第i城市。
  • 因此,根据上述观察,答案将是2 * Tsum的最大值

因此,从上面观察,发现数组元素/ 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)