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

📅  最后修改于: 2021-04-22 03:48:47             🧑  作者: Mango

给定两个数字NM分别表示1和0的计数,任务是最大化长度为3的二进制字符串的计数,该二进制字符串由0和1组成,它们可以由给定的N 1M形成0s
例子:

天真的方法:可以根据以下条件形成三个长度的二进制字符串:

  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

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

Java脚本


输出:
4

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