📜  排列二进制字符串以获取索引范围内的最大值

📅  最后修改于: 2021-06-26 19:48:45             🧑  作者: Mango

给定一个仅包含0和1的字符串。现在,您将获得N个非相交范围L,R(L <= R),更具体地说是[L1,R1],[L2,R2],…,[LN,RN],这些间隔中没有两个重叠-形式上,对于每个有效i,j,使得i!= j,Ri 任务是找到一个有效的排列,该排列将同时满足以下两个条件:

  1. 所有N个给定范围之间的数字总和将为最大。
  2. 该字符串在字典上最大。字符串1100在字典上比字符串1001大。

例子:

方法

  • 赋予第一优先权是使所有l和r之间的1的计数最大。我们计算数组中1的数目并存储在变量中。
  • 输入后,我们将每个l和r的范围都更新为1,以首先标记要填充1的位置。
  • 然后,我们获取数组的前缀和,以便获得第一个固定位置1的位置。然后,我们从左在该前缀和数组中运行一个循环。如果我们得到的任何头寸的值都大于1,则意味着该索引中有一个lr。我们继续将1放入这些索引中,直到1的计数变为零为止。
  • 现在,在最大化操作完成之后,如果还剩1,那么我们开始进行字典词典最大化。如果发现索引值为0表示没有lr索引,则再次从前缀求和数组的左侧开始循环,然后将1放入该索引,然后继续进行直到所有剩余的1都被填充为止。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
void arrange(string s) 
{
    int cc = 0;
  
    // Storing the count of 1's in the string
    for (int i = 0; i < s.length(); i++)
    {
        if (s[i] == '1') cc++;
    }
  
    int a[s.length() + 1] = {0};
  
    // Query of l and r
    int qq[][2] = {{2, 3}, {5, 5}};
  
    int n = sizeof(qq) / sizeof(qq[0]);
    for (int i = 0; i < n; i++)
    {
        int l = qq[i][0], r = qq[i][1];
        l--, r--;
        a[l]++;
  
        // Applying range update technique.
        a[r + 1]--;
    }
  
    int len_a = sizeof(a) / sizeof(a[0]);
    for (int i = 1; i < len_a; i++) 
    {
        // Taking prefix sum to get the range update values
        a[i] += a[i - 1];
    }
  
    // Final array which will store the arranged string
    int zz[s.length()] = {0};
  
    for (int i = 0; i < len_a - 1; i++)
    {
        if (a[i] > 0)
        {
            if (cc > 0) 
            {
                zz[i] = 1;
                cc--;
            } 
            else
                break;
        }
  
        if (cc == 0) break;
    }
  
    // if after maximizing the ranges any 1 is left
    // then we maximize the string lexicographically.
    if (cc > 0)
    {
        for (int i = 0; i < s.length(); i++) 
        {
            if (zz[i] == 0) 
            {
                zz[i] = 1;
                cc--;
            }
            if (cc == 0) break;
        }
    }
  
    for (int i = 0; i < s.length(); i++) 
        cout << zz[i];
    cout << endl;
}
  
// Driver Code
int main() 
{
    string str = "11100";
    arrange(str);
    return 0;
}
  
// This code is contributed by
// sanjeev2552


Java
// Java implementation of the approach
class GFG
{
  
static void arrange(String s) 
{
    int cc = 0;
  
    // Storing the count of 1's in the String
    for (int i = 0; i < s.length(); i++)
    {
        if (s.charAt(i) == '1') cc++;
    }
  
    int []a = new int[s.length() + 1];
  
    // Query of l and r
    int qq[][] = {{2, 3}, {5, 5}};
  
    int n = qq.length;
    for (int i = 0; i < n; i++)
    {
        int l = qq[i][0], r = qq[i][1];
        l--; r--;
        a[l]++;
  
        // Applying range update technique.
        a[r + 1]--;
    }
  
    int len_a = a.length;
    for (int i = 1; i < len_a; i++) 
    {
        // Taking prefix sum to get the range update values
        a[i] += a[i - 1];
    }
  
    // Final array which will store the arranged String
    int []zz = new int[s.length()];
  
    for (int i = 0; i < len_a - 1; i++)
    {
        if (a[i] > 0)
        {
            if (cc > 0) 
            {
                zz[i] = 1;
                cc--;
            } 
            else
                break;
        }
  
        if (cc == 0) break;
    }
  
    // if after maximizing the ranges any 1 is left
    // then we maximize the String lexicographically.
    if (cc > 0)
    {
        for (int i = 0; i < s.length(); i++) 
        {
            if (zz[i] == 0) 
            {
                zz[i] = 1;
                cc--;
            }
            if (cc == 0) break;
        }
    }
    for (int i = 0; i < s.length(); i++) 
        System.out.print(zz[i]);
    System.out.println();
}
  
// Driver Code
public static void main(String[] args) 
{
    String str = "11100";
    arrange(str);
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python implementation of the approach
def arrange(s):
    cc = 0
  
    # Storing the count of 1's in the string
    for i in range(len(s)): 
        if(s[i]=="1"):
            cc+= 1
    a =[0]*(len(s)+1)
  
    # Query of l and r
    qq =[(2, 3), (5, 5)]
  
    n = len(qq)
    for i in range(n):
        l, r = qq[i][0], qq[i][1]
        l-= 1
        r-= 1
        a[l]+= 1
  
        # Applying range update technique.
        a[r + 1]-= 1
  
    for i in range(1, len(a)):
  
        # Taking prefix sum to get the range update values
        a[i]= a[i]+a[i-1] 
  
    # Final array which will store the arranged string
    zz =[0]*len(s) 
  
    for i in range(len(a)-1):
        if(a[i]>0):
            if(cc>0):
                zz[i]= 1
                cc-= 1
            else:
                break
        if(cc == 0):
            break
  
    # if after maximizing the ranges any 1 is left
    # then we maximize the string lexicographically.
    if(cc>0): 
  
        for i in range(len(s)):
            if(zz[i]== 0):
                zz[i]= 1
                cc-= 1
            if(cc == 0):
                break
    print(*zz, sep ="")
  
str = "11100"
arrange(str)


C#
// C# implementation of the approach
using System;
  
class GFG
{
  
static void arrange(String s) 
{
    int cc = 0;
  
    // Storing the count of 1's in the String
    for (int i = 0; i < s.Length; i++)
    {
        if (s[i] == '1') cc++;
    }
  
    int []a = new int[s.Length + 1];
  
    // Query of l and r
    int [,]qq = {{2, 3}, {5, 5}};
  
    int n = qq.GetLength(0);
    for (int i = 0; i < n; i++)
    {
        int l = qq[i, 0], r = qq[i, 1];
        l--; r--;
        a[l]++;
  
        // Applying range update technique.
        a[r + 1]--;
    }
  
    int len_a = a.Length;
    for (int i = 1; i < len_a; i++) 
    {
        // Taking prefix sum to get the range update values
        a[i] += a[i - 1];
    }
  
    // Final array which will store the arranged String
    int []zz = new int[s.Length];
  
    for (int i = 0; i < len_a - 1; i++)
    {
        if (a[i] > 0)
        {
            if (cc > 0) 
            {
                zz[i] = 1;
                cc--;
            } 
            else
                break;
        }
  
        if (cc == 0) break;
    }
  
    // if after maximizing the ranges any 1 is left
    // then we maximize the String lexicographically.
    if (cc > 0)
    {
        for (int i = 0; i < s.Length; i++) 
        {
            if (zz[i] == 0) 
            {
                zz[i] = 1;
                cc--;
            }
            if (cc == 0) break;
        }
    }
    for (int i = 0; i < s.Length; i++) 
        Console.Write(zz[i]);
    Console.WriteLine();
}
  
// Driver Code
public static void Main(String[] args) 
{
    String str = "11100";
    arrange(str);
}
}
  
// This code is contributed by PrinciRaj1992


输出:
01101

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。