📌  相关文章
📜  最大化长度为 3 的字符串的数量,该字符串可以由 N 个 1 和 M 个 0 组成

📅  最后修改于: 2021-10-26 07:01:57             🧑  作者: Mango

给定两个分别表示 1 和 0 计数的数字NM ,任务是最大化长度为 3 的二进制字符串的计数,其中包含 0 和 1,可以从给定的N 个 1M 形成0 秒
例子:

朴素的方法:可以根据以下条件形成三种长度的二进制字符串:

  1. 如果 N > M:如果 N > 2,则将 N 减少 2,M 减少 1,并且由于生成了类型为110的字符串,因此将计数增加 1。
  2. 如果N≤M:如果M>2,则将M减2,N减1,由于生成的是001类型的字符串,因此将计数加1。

因此,想法是迭代一个循环,直到 N 或 M 变为零,并根据上述条件不断更新字符串的计数。
下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function that counts the number of
// strings of length three that can be
// made with given m 0s and n 1s
void number_of_strings(int N, int M)
{
    int ans = 0;
 
    // Iterate until N & M are positive
    while (N > 0 && M > 0) {
 
        // Case 1:
        if (N > M) {
            if (N >= 2) {
                N -= 2;
                --M;
                ++ans;
            }
            else {
                break;
            }
        }
 
        // Case 2:
        else {
            if (M >= 2) {
                M -= 2;
                --N;
                ++ans;
            }
            else {
                break;
            }
        }
    }
 
    // Print the count of strings
    cout << ans;
}
 
// Driver Code
int main()
{
    // Given count of 1s and 0s
    int N = 4, M = 19;
 
    // Function Call
    number_of_strings(N, M);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
import java.lang.*;
 
class GFG{
     
// Function that counts the number of
// strings of length three that can be
// made with given m 0s and n 1s
static void number_of_strings(int N, int M)
{
    int ans = 0;
 
    // Iterate until N & M are positive
    while (N > 0 && M > 0)
    {
         
        // Case 1:
        if (N > M)
        {
            if (N >= 2)
            {
                N -= 2;
                --M;
                ++ans;
            }
            else
            {
                break;
            }
        }
         
        // Case 2:
        else
        {
            if (M >= 2)
            {
                M -= 2;
                --N;
                ++ans;
            }
            else
            {
                break;
            }
        }
    }
     
    // Print the count of strings
    System.out.println(ans);
}
 
// Driver Code
public static void main (String[] args)
{
     
    // Given count of 1s and 0s
    int N = 4, M = 19;
 
    // Function call
    number_of_strings(N, M);
}
}
 
// This code is contributed by jana_sayantan


Python3
# Python3 program for the above approach
 
# Function that counts the number of
# strings of length three that can be
# made with given m 0s and n 1s
def number_of_strings(N, M):
 
    ans = 0
 
    # Iterate until N & M are positive
    while (N > 0 and M > 0):
 
        # Case 1:
        if (N > M):
            if (N >= 2):
                N -= 2
                M -= 1
                ans += 1
             
            else:
                break
             
        # Case 2:
        else:
            if M >= 2:
                M -= 2
                N -= 1
                ans += 1
             
            else:
                break
         
    # Print the count of strings
    print(ans)
 
# Driver code
if __name__ == '__main__':
 
    # Given count of 1s and 0s
    N = 4
    M = 19
 
    # Function call
    number_of_strings(N, M)
 
# This code is contributed by jana_sayantan


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function that counts the number of
// strings of length three that can be
// made with given m 0s and n 1s
static void number_of_strings(int N, int M)
{
    int ans = 0;
 
    // Iterate until N & M are positive
    while (N > 0 && M > 0)
    {
         
        // Case 1:
        if (N > M)
        {
            if (N >= 2)
            {
                N -= 2;
                --M;
                ++ans;
            }
            else
            {
                break;
            }
        }
 
        // Case 2:
        else
        {
            if (M >= 2)
            {
                M -= 2;
                --N;
                ++ans;
            }
            else
            {
                break;
            }
        }
    }
     
    // Print the count of strings
    Console.WriteLine(ans);
}
 
// Driver Code
public static void Main (String[] args)
{
     
    // Given count of 1s and 0s
    int N = 4, M = 19;
 
    // Function call
    number_of_strings(N, M);
}
}
 
// This code is contributed by jana_sayantan


Javascript


C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function that counts the number of
// strings of length 3 that can be
// made with given m 0s and n 1s
void number_of_strings(int N, int M)
{
 
    // Print the count of strings
    cout << min(N, min(M, (N + M) / 3));
}
 
// Driver Code
int main()
{
    // Given count of 1s and 0s
    int N = 4, M = 19;
 
    // Function Call
    number_of_strings(N, M);
    return 0;
}


Java
// Java program for
// the above approach
class GFG{
 
// Function that counts the number of
// Strings of length 3 that can be
// made with given m 0s and n 1s
static void number_of_Strings(int N, int M)
{
  // Print the count of Strings
  System.out.print(Math.min(N,
                            Math.min(M,
                                    (N + M) /
                                     3)));
}
 
// Driver Code
public static void main(String[] args)
{
  // Given count of 1s and 0s
  int N = 4, M = 19;
 
  // Function Call
  number_of_Strings(N, M);
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program for the above approach
 
# Function that counts the number of
# strings of length 3 that can be
# made with given m 0s and n 1s
def number_of_strings(N, M):
 
    # Print the count of strings
    print(min(N, min(M, (N + M) // 3)))
 
# Driver Code
if __name__ == '__main__':
 
    # Given count of 1s and 0s
    N = 4
    M = 19
 
    # Function call
    number_of_strings(N, M)
 
# This code is contributed by mohit kumar 29


C#
// C# program for
// the above approach
using System;
class GFG{
 
// Function that counts the number of
// Strings of length 3 that can be
// made with given m 0s and n 1s
static void number_of_Strings(int N,
                              int M)
{
  // Print the count of Strings
  Console.Write(Math.Min(N,
                Math.Min(M, (N + M) / 3)));
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given count of 1s and 0s
  int N = 4, M = 19;
 
  // Function Call
  number_of_Strings(N, M);
}
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:

4

时间复杂度: O(max(A, B))
辅助空间: O(1)
有效的方法:为了优化上述方法,观察可以形成的二进制字符串的总数至少为 N、M 和 (N + M)/3,如下所示:

  • 如果 N 是最小值,并且我们有 M ≥ 2*N,那么所有类型为110的字符串都可以生成。
  • 如果 M 是最小值,并且我们有 N ≥ 2*M 则可以制作所有类型为001的字符串。
  • 否则总计数将为 (N + M)/3,并且可以生成110001类型的字符串。

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
using namespace std;
 
// Function that counts the number of
// strings of length 3 that can be
// made with given m 0s and n 1s
void number_of_strings(int N, int M)
{
 
    // Print the count of strings
    cout << min(N, min(M, (N + M) / 3));
}
 
// Driver Code
int main()
{
    // Given count of 1s and 0s
    int N = 4, M = 19;
 
    // Function Call
    number_of_strings(N, M);
    return 0;
}

Java

// Java program for
// the above approach
class GFG{
 
// Function that counts the number of
// Strings of length 3 that can be
// made with given m 0s and n 1s
static void number_of_Strings(int N, int M)
{
  // Print the count of Strings
  System.out.print(Math.min(N,
                            Math.min(M,
                                    (N + M) /
                                     3)));
}
 
// Driver Code
public static void main(String[] args)
{
  // Given count of 1s and 0s
  int N = 4, M = 19;
 
  // Function Call
  number_of_Strings(N, M);
}
}
 
// This code is contributed by shikhasingrajput

蟒蛇3

# Python3 program for the above approach
 
# Function that counts the number of
# strings of length 3 that can be
# made with given m 0s and n 1s
def number_of_strings(N, M):
 
    # Print the count of strings
    print(min(N, min(M, (N + M) // 3)))
 
# Driver Code
if __name__ == '__main__':
 
    # Given count of 1s and 0s
    N = 4
    M = 19
 
    # Function call
    number_of_strings(N, M)
 
# This code is contributed by mohit kumar 29

C#

// C# program for
// the above approach
using System;
class GFG{
 
// Function that counts the number of
// Strings of length 3 that can be
// made with given m 0s and n 1s
static void number_of_Strings(int N,
                              int M)
{
  // Print the count of Strings
  Console.Write(Math.Min(N,
                Math.Min(M, (N + M) / 3)));
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given count of 1s and 0s
  int N = 4, M = 19;
 
  // Function Call
  number_of_Strings(N, M);
}
}
 
// This code is contributed by shikhasingrajput

Javascript


输出:
4

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程