📜  可以使用给定单位 a 和 b 交叉的最大元素

📅  最后修改于: 2021-10-26 06:55:06             🧑  作者: Mango

给定一个包含N 个元素的二进制数组和两个初始值 a 和 b。如果满足以下条件,我们可以交叉第 i 个元素:

  1. 如果 a[i] == 0 ,那么我们可以使用 b 或 a 中的 1 个单位来穿过第 i 个元素。
  2. 如果a[i] == 1 ,那么如果我们使用 b 中的 1 个单位,则 a 增加 1 个单位。如果从 a 使用 1 个单位,则 a 或 b 都没有增加。

任务是找到可以使用 a 和 b 单位交叉的最大元素数。
注意:当我们在任何一步将 a 增加 1 时,它都不能超过 a 的原始值。
例子:

方法:迭代数组元素并执行以下步骤:

  • 如果我们没有 b 或 a 来传递元素,则中断。
  • 否则,如果没有 a,则使用 b,如果 arr[i] == 1,则将 a 增加 1。
  • 否则,如果没有 b 就使用 a。
  • 否则,如果 arr[i]==1 则使用 b 并将 a 增加 1 直到原始 a 的最大值。
  • 否则,只需使用 a 中的 1 个单位。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to find the number
// of elements crossed
int findElementsCrossed(int arr[], int a, int b, int n)
{
    // Keep a copy of a
    int aa = a;
    int ans = 0;
 
    // Iterate in the binary array
    for (int i = 0; i < n; i++) {
 
        // If no a and b left to use
        if (a == 0 && b == 0)
            break;
 
        // If there is no a
        else if (a == 0) {
 
            // use b and increase a by 1
            // if arr[i] is 1
            if (arr[i] == 1) {
                b -= 1;
                a = min(aa, a + 1);
            }
 
            // simply use b
            else
                b -= 1;
        }
 
        // Use a if theres no b
        else if (b == 0)
            a--;
 
        // Increase a and use b if arr[i] == 1
        else if (arr[i] == 1 && a < aa) {
            b -= 1;
            a = min(aa, a + 1);
        }
 
        // Use a
        else
            a--;
        ans++;
    }
 
    return ans;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 0, 0, 1, 0, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int a = 1;
    int b = 2;
    cout << findElementsCrossed(arr, a, b, n);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG
{
 
// Function to find the number
// of elements crossed
static int findElementsCrossed(int arr[],
                        int a, int b, int n)
{
    // Keep a copy of a
    int aa = a;
    int ans = 0;
 
    // Iterate in the binary array
    for (int i = 0; i < n; i++)
    {
 
        // If no a and b left to use
        if (a == 0 && b == 0)
            break;
 
        // If there is no a
        else if (a == 0)
        {
 
            // use b and increase a by 1
            // if arr[i] is 1
            if (arr[i] == 1)
            {
                b -= 1;
                a = Math.min(aa, a + 1);
            }
 
            // simply use b
            else
                b -= 1;
        }
 
        // Use a if theres no b
        else if (b == 0)
            a--;
 
        // Increase a and use b if arr[i] == 1
        else if (arr[i] == 1 && a < aa)
        {
            b -= 1;
            a = Math.min(aa, a + 1);
        }
 
        // Use a
        else
            a--;
        ans++;
    }
 
    return ans;
}
 
// Driver code
public static void main(String args[])
{
    int arr[] = { 1, 0, 0, 1, 0, 1 };
    int n = arr.length;
    int a = 1;
    int b = 2;
    System.out.println(findElementsCrossed(arr, a, b, n));
 
}
}
 
// This code is contributed by
// Surendra_Gangwar


Python3
# Python3 program to implement
# the above approach
 
# Function to find the number
# of elements crossed
def findElementsCrossed(arr, a, b, n):
 
    # Keep a copy of a
    aa = a
    ans = 0
 
    # Iterate in the binary array
    for i in range(n):
 
        # If no a and b left to use
        if (a == 0 and b == 0):
            break
 
        # If there is no a
        elif (a == 0):
 
            # use b and increase a by 1
            # if arr[i] is 1
            if (arr[i] == 1):
                b -= 1
                a = min(aa, a + 1)
             
            # simply use b
            else:
                b -= 1
         
        # Use a if theres no b
        elif (b == 0):
            a -= 1
 
        # Increase a and use b if arr[i] == 1
        elif (arr[i] == 1 and a < aa):
            b -= 1
            a = min(aa, a + 1)
         
        # Use a
        else:
            a -= 1
        ans += 1
     
    return ans
 
# Driver code
arr = [1, 0, 0, 1, 0, 1]
n = len(arr)
a = 1
b = 2
print(findElementsCrossed(arr, a, b, n))
 
# This code is contributed by mohit kumar


C#
// C# implementation of the above approach
using System;
 
class GFG
{
 
// Function to find the number
// of elements crossed
static int findElementsCrossed(int []arr,
                        int a, int b, int n)
{
    // Keep a copy of a
    int aa = a;
    int ans = 0;
 
    // Iterate in the binary array
    for (int i = 0; i < n; i++)
    {
 
        // If no a and b left to use
        if (a == 0 && b == 0)
            break;
 
        // If there is no a
        else if (a == 0)
        {
 
            // use b and increase a by 1
            // if arr[i] is 1
            if (arr[i] == 1)
            {
                b -= 1;
                a = Math.Min(aa, a + 1);
            }
 
            // simply use b
            else
                b -= 1;
        }
 
        // Use a if theres no b
        else if (b == 0)
            a--;
 
        // Increase a and use b if arr[i] == 1
        else if (arr[i] == 1 && a < aa)
        {
            b -= 1;
            a = Math.Min(aa, a + 1);
        }
 
        // Use a
        else
            a--;
        ans++;
    }
 
    return ans;
}
 
// Driver code
public static void Main(String []args)
{
    int []arr = { 1, 0, 0, 1, 0, 1 };
    int n = arr.Length;
    int a = 1;
    int b = 2;
    Console.WriteLine(findElementsCrossed(arr, a, b, n));
 
}
}
 
// This code contributed by Rajput-Ji


PHP


Javascript


输出:
3

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