📌  相关文章
📜  重新排列数组以根据给定条件最大化具有Array元素为数字的数量

📅  最后修改于: 2021-05-06 09:58:40             🧑  作者: Mango

给定一个整数数组arr []和一个长度为N的二进制字符串str ,任务是通过交换字符串具有相同字符的索引中的数组元素来重新排列给定的数组,从而使由重新排列的数组的元素形成的数字因为数字是最大可能的。

例子:

方法:按照以下步骤解决问题:

  1. 创建两个数组以存储该数组中的0个字符的索引元素和1个字符的索引元素。
  2. 对这两个数组进行排序,以形成最大可能的数字。
  3. 遍历str并基于字符,从已排序的数组中放置数组元素。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Comparison Function to sort()
int myCompare(int a, int b)
{
    string X = to_string(a);
    string Y = to_string(b);
 
    // Append Y at the end of X
    string XY = X.append(Y);
 
    // Append X at the end of Y
    string YX = Y.append(X);
 
    // Compare and return greater
    return XY.compare(YX) < 0 ? 1 : 0;
}
 
// Function to return the rearranged
// array in the form of largest
// possible number that can be formed
void findMaxArray(vector& arr, string& str)
{
    int N = arr.size();
    vector Z, O, ans(N);
 
    for (int i = 0; i < N; i++) {
        if (str[i] == '0') {
            Z.push_back(arr[i]);
        }
 
        else {
            O.push_back(arr[i]);
        }
    }
 
    // Sort them in decreasing order
    sort(Z.rbegin(), Z.rend(), myCompare);
    sort(O.rbegin(), O.rend(), myCompare);
 
    int j = 0, k = 0;
 
    // Generate the sorted array
    for (int i = 0; i < N; i++) {
        if (str[i] == '0') {
            ans[i] = Z[j++];
        }
        else {
            ans[i] = O[k++];
        }
    }
 
    for (int i = 0; i < N; i++) {
        cout << ans[i] << " ";
    }
}
 
// Driver Code
int main()
{
    vector arr = { 1, 3, 456, 6, 7, 8 };
    string str = "101101";
    findMaxArray(arr, str);
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
import java.lang.*;
 
class GFG{
 
// Function to return the rearranged
// array in the form of largest
// possible number that can be formed
static void findMaxArray(int[] arr, String str)
{
    int N = arr.length;
    ArrayList Z = new ArrayList<>(),
                       O = new ArrayList<>();
                        
    int[] ans = new int[N];
 
    for(int i = 0; i < N; i++)
    {
        if (str.charAt(i) == '0')
        {
            Z.add(arr[i]);
        }
        else
        {
            O.add(arr[i]);
        }
    }
 
    // Sort them in decreasing order
    Collections.sort(Z, new Comparator()
    {
        public int compare(Integer a, Integer b)
        {
            String X = Integer.toString(a);
            String Y = Integer.toString(b);
             
            // Append Y at the end of X
            String XY = X + Y;
         
            // Append X at the end of Y
            String YX = Y + X;
         
            // Compare and return greater
            return XY.compareTo(YX) > 0 ? -1 : 1;
        }
    });
     
    Collections.sort(O, new Comparator()
    {
        public int compare(Integer a, Integer b)
        {
            String X = Integer.toString(a);
            String Y = Integer.toString(b);
 
            // Append Y at the end of X
            String XY = X + Y;
         
            // Append X at the end of Y
            String YX = Y + X;
         
            // Compare and return greater
            return XY.compareTo(YX) > 0 ? -1 : 1;
        }
    });
     
    int j = 0, k = 0;
 
    // Generate the sorted array
    for(int i = 0; i < N; i++)
    {
        if (str.charAt(i) == '0')
        {
            ans[i] = Z.get(j++);
        }
        else
        {
            ans[i] = O.get(k++);
        }
    }
 
    for(int i = 0; i < N; i++)
    {
        System.out.print(ans[i] + " ");
    }
}
 
// Driver code
public static void main (String[] args)
{
    int[] arr = { 1, 3, 456, 6, 7, 8 };
    String str = "101101";
     
    findMaxArray(arr, str);
}
}
 
// This code is contributed by offbeat


输出:
8 7 6 456 3 1




时间复杂度: O(NlogN)
辅助空间: O(N)