通过重新排列 Array 使第一个和最后一个元素的绝对差最小化来最大化分数
给定一个大小为N的数组arr[] ,任务是重新排列数组以达到可能的最大分数,并使第一个和最后一个元素的绝对差尽可能小。分数分布如下:
- 如果arr[i] < arr[i+1] ,则将 score 增加1 。
- 否则保持分数不变。
例子:
Input: N = 5, arr[] = {5, 7, 9, 5, 8}
Output: {5, 7, 8, 9, 5}
Explanation: After rearranging the array, absolute difference of first and last element = abs(5 – 5) = 0, which is the minimum possible.
Maximum score that can be achieved :
- arr[0] < arr[1], score = 1
- arr[1] < arr[2], score = 2
- arr[2] > arr[3], score = 3
- arr[3] > arr[4], score = 3
Input: N = 4, arr[] = {6, 4, 1, 3}
Output: {3, 6, 1, 4}
方法:给定的问题可以通过贪心方法来解决。请按照以下步骤解决问题:
- 对数组进行排序并找到每对相邻元素之间的最小绝对差,并存储它的索引,例如ind 。
- 现在,仅使用在索引处( ind – 1和ind )与这两个元素分开的元素。这两个元素将是所需数组的第一个和最后一个元素。
- 通过以下两种方式存储剩余元素来解决问题:
- 首先在 res 数组中存储大于索引(ind – 1)处的元素的元素。
- 然后,将小于或等于索引(ind) 处元素的元素存储在 res 数组中。
- 最后,压入最后一个元素,即索引(ind)处的元素,并返回res向量。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
// Function to rearrange the array to
// achieve the maximum score
vector rearrangeArray(vector arr)
{
int min_diff = INT_MAX;
int ind = -1;
int n = arr.size();
// Sort the array
sort(arr.begin(), arr.end());
// Iterate the array and find the
// minimum difference between two
// elements along with its index
for (int i = 1; i < n; i++) {
if (abs(arr[i] - arr[i - 1]) < min_diff) {
min_diff = abs(arr[i] - arr[i - 1]);
ind = i;
}
}
// Vector to store the rearranged
// elements
vector res;
// Push the element at (ind - 1)
res.push_back(arr[ind - 1]);
// Traverse the array and push the
// elements in res which are greater
// than the element at index (ind - 1)
for (int i = 0; i < n; i++) {
if (i == ind || i == ind - 1) {
continue;
}
if (arr[i] > arr[ind - 1]) {
res.push_back(arr[i]);
}
}
// Again traverse the array and push the
// elements in res which are less or equal
// to the element at index (ind)
for (int i = 0; i < n; i++) {
if (i == ind || i == ind - 1) {
continue;
}
if (arr[i] <= arr[ind]) {
res.push_back(arr[i]);
}
}
// At the end, push the element at (ind)
res.push_back(arr[ind]);
// Return res vector
return res;
}
// Driver Code
int main()
{
vector arr = { 5, 7, 9, 5, 8 };
vector res = rearrangeArray(arr);
for (auto it : res)
cout << it << " ";
return 0;
}
Java
// Java implementation of the above approach
import java.util.*;
public class GFG
{
// Function to rearrange the array to
// achieve the maximum score
static ArrayList rearrangeArray(ArrayList arr)
{
int min_diff = Integer.MAX_VALUE;
int ind = -1;
int n = arr.size();
// Sort the array
Collections.sort(arr);
// Iterate the array and find the
// minimum difference between two
// elements along with its index
for (int i = 1; i < n; i++) {
if (Math.abs(arr.get(i) - arr.get(i - 1)) < min_diff) {
min_diff = Math.abs(arr.get(i) - arr.get(i - 1));
ind = i;
}
}
// Vector to store the rearranged
// elements
ArrayList res = new ArrayList();
// Push the element at (ind - 1)
res.add(arr.get(ind - 1));
// Traverse the array and push the
// elements in res which are greater
// than the element at index (ind - 1)
for (int i = 0; i < n; i++) {
if (i == ind || i == ind - 1) {
continue;
}
if (arr.get(i) > arr.get(ind - 1)) {
res.add(arr.get(i));
}
}
// Again traverse the array and push the
// elements in res which are less or equal
// to the element at index (ind)
for (int i = 0; i < n; i++) {
if (i == ind || i == ind - 1) {
continue;
}
if (arr.get(i) <= arr.get(ind)) {
res.add(arr.get(i));
}
}
// At the end, push the element at (ind)
res.add(arr.get(ind));
// Return res
return res;
}
// Driver Code
public static void main(String args[])
{
ArrayList arr
= new ArrayList();
arr.add(5);
arr.add(7);
arr.add(9);
arr.add(5);
arr.add(8);
ArrayList res = rearrangeArray(arr);
for (int i = 0; i < res.size(); i++) {
System.out.print(res.get(i) + " ");
}
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python code for the above approach
# Function to rearrange the array to
# achieve the maximum score
def rearrangeArray(arr):
min_diff = 9999999
ind = -1
n = len(arr)
# Sort the array
arr.sort()
# Iterate the array and find the
# minimum difference between two
# elements along with its index
for i in range(n):
if abs(arr[i] - arr[i - 1]) < min_diff:
min_diff = abs(arr[i] - arr[i - 1])
ind = i
# Vector to store the rearranged
# elements
res = []
# Push the element at (ind - 1)
res.append(arr[ind - 1])
# Traverse the array and push the
# elements in res which are greater
# than the element at index (ind - 1)
for i in range(n):
if i == ind or i == ind - 1:
continue
if arr[i] > arr[ind - 1]:
res.append(arr[i])
# Again traverse the array and push the
# elements in res which are less or equal
# to the element at index (ind)
for i in range(n):
if i == ind or i == ind - 1 :
continue;
if arr[i] <= arr[ind] :
res.append(arr[i])
# At the end, push the element at (ind)
res.append(arr[ind])
# Return res vector
return res
# Driver Code
arr = [ 5, 7, 9, 5, 8 ]
res = rearrangeArray(arr)
for i in range(len(res)):
print(str(res[i]),end =" ")
# This code is contributed by Potta Lokesh
C#
// C# implementation of the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to rearrange the array to
// achieve the maximum score
static List rearrangeArray(List arr)
{
int min_diff = Int32.MaxValue;
int ind = -1;
int n = arr.Count;
// Sort the array
arr.Sort();
// Iterate the array and find the
// minimum difference between two
// elements along with its index
for (int i = 1; i < n; i++) {
if (Math.Abs(arr[i] - arr[i - 1]) < min_diff) {
min_diff = Math.Abs(arr[i] - arr[i - 1]);
ind = i;
}
}
// Vector to store the rearranged
// elements
List res = new List();
// Push the element at (ind - 1)
res.Add(arr[ind - 1]);
// Traverse the array and push the
// elements in res which are greater
// than the element at index (ind - 1)
for (int i = 0; i < n; i++) {
if (i == ind || i == ind - 1) {
continue;
}
if (arr[i] > arr[ind - 1]) {
res.Add(arr[i]);
}
}
// Again traverse the array and push the
// elements in res which are less or equal
// to the element at index (ind)
for (int i = 0; i < n; i++) {
if (i == ind || i == ind - 1) {
continue;
}
if (arr[i] <= arr[ind]) {
res.Add(arr[i]);
}
}
// At the end, push the element at (ind)
res.Add(arr[ind]);
// Return res
return res;
}
// Driver Code
public static void Main()
{
List arr
= new List();
arr.Add(5);
arr.Add(7);
arr.Add(9);
arr.Add(5);
arr.Add(8);
List res = rearrangeArray(arr);
foreach(int k in res)
Console.Write(k + " ");
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
5 7 8 9 5
时间复杂度: O(n logn)
辅助空间: O(n)