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

📅  最后修改于: 2021-09-07 02:11:04             🧑  作者: Mango

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

例子:

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

  • 假设i城市的访问时间最长为T ,一个人访问所有城市的总时间是所有数组元素的总和,比如sum
  • 如果第一人参观i城市,那么在时间T,第二个人将参观在那个时候其他城市,如果可能的话。
  • 如果值T至多(sum – T) ,则两个人可以在sum时间内单独访问该地点。
  • 否则,第二人将不得不等待参观的i城市。那么,所需的总时间将是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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live