基于给定二进制字符串的 [1, N] 的字典最小排列
给定一个大小为(N – 1)的二进制字符串S ,任务是找到前N个自然数的字典序最小排列P ,使得对于每个索引i ,如果S[i]等于 ' 0 ',则P[i + 1]必须大于P[i]并且如果S[i]等于 ' 1 ' 则P[i + 1]必须小于P[i] 。
例子:
Input: N = 7, S = 100101
Output: 2 1 3 5 4 7 6
Explanation:
Consider the permutation as {2, 1, 3, 5, 4, 7, 6} that satisfy the given criteria as:
For index 0, S[0] = 1, P[1] < P[0], i.e. 1 < 2
For index 1, S[1] = 0, P[2] < P[1], i.e. 3 > 1
For index 2, S[2] = 0, P[3] < P[2], i.e. 5 > 3
For index 3, S[3] = 1, P[4] < P[3], i.e. 4 < 5
For index 4, S[4] = 0, P[5] < P[4], i.e. 7 > 4
For index 5, S[5] = 1, P[6] < P[5], i.e. 6 < 7
Input: N = 4, S = 000
Output: 1 2 3 4
方法:给定的问题可以通过使用贪婪方法来解决,尽可能在较低的索引处使用较小的数字将创建字典上最小的排列。请按照以下步骤解决问题:
- 初始化一个数组,比如ans[] ,大小为N ,存储结果排列。
- 遍历给定的字符串S并执行以下步骤:
- 如果S[i]的值等于“ 0 ”,则分配大于最后分配的数字的数字。
- 否则,分配比所有当前使用的数字大的最小数字。
- 完成上述步骤后,打印在数组ans[]中形成的结果排列。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to generate the lexicographically
// smallest permutation according to the
// given criteria
void constructPermutation(string S, int N)
{
// Stores the resultant permutation
int ans[N];
// Initialize the first elements to 1
ans[0] = 1;
// Traverse the given string S
for (int i = 1; i < N; ++i) {
if (S[i - 1] == '0') {
// Number greater than last
// number
ans[i] = i + 1;
}
else {
// Number equal to the last
// number
ans[i] = ans[i - 1];
}
// Correct all numbers to the left
// of the current index
for (int j = 0; j < i; ++j) {
if (ans[j] >= ans[i]) {
ans[j]++;
}
}
}
// Printing the permutation
for (int i = 0; i < N; i++) {
cout << ans[i];
if (i != N - 1) {
cout << " ";
}
}
}
// Driver Code
int main()
{
string S = "100101";
constructPermutation(S, S.length() + 1);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to generate the lexicographically
// smallest permutation according to the
// given criteria
static void constructPermutation(String S, int N)
{
// Stores the resultant permutation
int[] ans = new int[N];
// Initialize the first elements to 1
ans[0] = 1;
// Traverse the given string S
for (int i = 1; i < N; ++i) {
if (S.charAt(i - 1) == '0') {
// Number greater than last
// number
ans[i] = i + 1;
}
else {
// Number equal to the last
// number
ans[i] = ans[i - 1];
}
// Correct all numbers to the left
// of the current index
for (int j = 0; j < i; ++j) {
if (ans[j] >= ans[i]) {
ans[j]++;
}
}
}
// Printing the permutation
for (int i = 0; i < N; i++) {
System.out.print(ans[i]);
if (i != N - 1) {
System.out.print(" ");
}
}
}
// Driver Code
public static void main(String[] args) {
String S = "100101";
constructPermutation(S, S.length() + 1);
}
}
// This code is contributed by code_hunt.
Python3
# Python Program to implement
# the above approach
# Function to generate the lexicographically
# smallest permutation according to the
# given criteria
def constructPermutation(S, N):
# Stores the resultant permutation
ans = [0] * N
# Initialize the first elements to 1
ans[0] = 1
# Traverse the given string S
for i in range(1, N):
if (S[i - 1] == '0'):
# Number greater than last
# number
ans[i] = i + 1
else :
# Number equal to the last
# number
ans[i] = ans[i - 1]
# Correct all numbers to the left
# of the current index
for j in range(i):
if (ans[j] >= ans[i]):
ans[j] += 1
# Printing the permutation
for i in range(N):
print(ans[i], end="")
if (i != N - 1):
print(" ", end="")
# Driver Code
S = "100101"
constructPermutation(S, len(S) + 1)
# This code is contributed by Saurabh Jaiswal
C#
// C# program for the above approach
using System;
class GFG {
// Function to generate the lexicographically
// smallest permutation according to the
// given criteria
static void constructPermutation(string S, int N)
{
// Stores the resultant permutation
int[] ans = new int[N];
// Initialize the first elements to 1
ans[0] = 1;
// Traverse the given string S
for (int i = 1; i < N; ++i) {
if (S[i - 1] == '0') {
// Number greater than last
// number
ans[i] = i + 1;
}
else {
// Number equal to the last
// number
ans[i] = ans[i - 1];
}
// Correct all numbers to the left
// of the current index
for (int j = 0; j < i; ++j) {
if (ans[j] >= ans[i]) {
ans[j]++;
}
}
}
// Printing the permutation
for (int i = 0; i < N; i++) {
Console.Write(ans[i]);
if (i != N - 1) {
Console.Write(" ");
}
}
}
// Driver Code
public static void Main()
{
string S = "100101";
constructPermutation(S, S.Length + 1);
}
}
// This code is contributed by ukasp.
Javascript
2 1 3 5 4 7 6
时间复杂度: O(N 2 )
辅助空间: O(N)