给定一个整数数组arr []和一个长度为N的二进制字符串str ,任务是通过交换字符串具有相同字符的索引中的数组元素来重新排列给定的数组,从而使由重新排列的数组的元素形成的数字因为数字是最大可能的。
例子:
Input: arr[]={1, 3, 4, 2}, str=”0101”
Output: 4 3 1 2
Explanation:
Since arr[0] is less than arr[2], so swap them. Therefore the maximum possible number from the array is 4, 3, 1, 2.
Input: arr[] = { 1, 3, 456, 6, 7, 8 }, str = “101101”
Output: 8 7 6 456 3 1
Explanation:
Array elements present at 0-chractered indices: {3, 7}
Largest number that can be formed from the above two numbers is 73
Array elements present at 1-chractered indices: {1, 456, 6, 8}
Largest number that can be formed from the above two numbers is 864561
Therefore, maximum number that can be generated from the array is 87645631
方法:按照以下步骤解决问题:
- 创建两个数组以存储该数组中的0个字符的索引元素和1个字符的索引元素。
- 对这两个数组进行排序,以形成最大可能的数字。
- 遍历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)