通过最小化每个元素,N-1 次操作中的最大数组最小值
给定一个包含N个整数的数组arr[] ,任务是在N-1 次删除操作后找到所有最小元素的最大值。在一个操作中,从数组中删除最小的元素,然后从所有剩余元素中减去它。
例子:
Input: arr[] = {-1, -2, 4, 3, 5}
Output: 4
Explanation: Following are the operations performed:
Operation 1: Remove -2 and subtract it from remaining. Now array arr[] becomes {1, 6, 5, 7}. minimum element =1, max minimum element = 1.
Operation 2: Remove 1 and subtract it from remaining. Now array arr[] becomes {5, 4, 6}. minimum element =4, max minimum element = 4.
Operation 3: Remove 4 and subtract it from the remaining. Now arr[] becomes {1, 2}. minimum element =1 max minimum element = 4 till now.
Operation 4: Remove 1 and subtract it from remaining. Now arr[] becomes {1}. minimum element = 1, max minimum element = 4 till now
Therefore, Maximum minimum element is 4.
Input: arr[] = {-3, -1, -6, -7}
Output: 3
朴素方法:从数组中删除最小元素并从剩余元素中进行减法,并在每次操作中跟踪数组的最小值的最大值,而数组的大小不等于 0。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法:上述方法可以通过使用贪心方法进行优化。这可以从数学上推导出来,因为每次都需要删除最小元素,因此它与数组中元素的顺序无关。所以需要对数组进行排序。按照下面的观察,解决问题:
Since the Minimum element needs to be removed in each operation. Consider the array after sorting in increasing order is {a1, a2, a3, a4, a5, …}
Initially a1 is the minimum and after removing it, the array becomes {a2-a1, a3-a1, a4-a1, a5-a1, …}
Now a2-a1 is the minimum and after removing it, the array becomes {a3-a1-(a2-a1), a4-a1-(a2-a1), …} which is equal to {a3-a2, a4-a2, a5-a2, …}
Now a3-a2 is the minimum and it continues so…
So, res = max(a1, ∑(i=0 to (N-1)) (ai+1 -ai))
因此,最终结果将是两个连续元素的最大差异,如上述证明所示。请按照以下步骤解决问题:
- 创建一个变量max_value ,用于存储最终答案并使用arr[0]对其进行初始化。
- 按升序对数组arr[]进行排序。
- 运行从i=0 到 i<(N-1)的循环,并且在每次迭代中:
- 在每次迭代中跟踪最小值的最大值(即差arr[i + 1] – arr[i] )。因此,将max_value和max_value和(arr[i+1] – arr[i])设为最大值。
- 返回max_value作为最终答案。
下面是上述方法的实现:
C++
// C++ code for the above approach
#include
using namespace std;
// Function to find maximum of minimum value
// of the array in the array
// in each operation
int maxOfAllMins(int arr[], int n)
{
// If no operations are done
int max_value = arr[0];
// Sort the array arr in ascending order
sort(arr, arr + n);
for (int i = 0; i < n - 1; i++) {
max_value = max(max_value,
arr[i + 1] - arr[i]);
}
return max_value;
}
// Driver code
int main()
{
int arr[] = { -1, -2, 4, 3, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << maxOfAllMins(arr, N);
return 0;
}
Java
// Java program for above approach
import java.util.*;
public class GFG {
// Function to find maximum of minimum value
// of the array in the array
// in each operation
static int maxOfAllMins(int[] arr, int n)
{
// If no operations are done
int max_value = arr[0];
// Sort the array arr in ascending order
Arrays.sort(arr);
for (int i = 0; i < n - 1; i++) {
max_value
= Math.max(max_value, arr[i + 1] - arr[i]);
}
return max_value;
}
// Driver code
public static void main(String args[])
{
int[] arr = { -1, -2, 4, 3, 5 };
int N = arr.length;
System.out.println(maxOfAllMins(arr, N));
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# python3 code for the above approach
# Function to find maximum of minimum value
# of the array in the array
# in each operation
def maxOfAllMins(arr, n):
# If no operations are done
max_value = arr[0]
# Sort the array arr in ascending order
arr.sort()
for i in range(0, n-1):
max_value = max(max_value,
arr[i + 1] - arr[i])
return max_value
# Driver code
if __name__ == "__main__":
arr = [-1, -2, 4, 3, 5]
N = len(arr)
print(maxOfAllMins(arr, N))
# This code is contributed by rakeshsahni
C#
// C# code for the above approach
using System;
class GFG {
// Function to find maximum of minimum value
// of the array in the array
// in each operation
static int maxOfAllMins(int[] arr, int n)
{
// If no operations are done
int max_value = arr[0];
// Sort the array arr in ascending order
Array.Sort(arr);
for (int i = 0; i < n - 1; i++) {
max_value
= Math.Max(max_value, arr[i + 1] - arr[i]);
}
return max_value;
}
// Driver code
public static void Main()
{
int[] arr = { -1, -2, 4, 3, 5 };
int N = arr.Length;
Console.WriteLine(maxOfAllMins(arr, N));
}
}
// This code is contributed by ukasp.
Javascript
4
时间复杂度: O(NlogN)
辅助空间: O(1)