📌  相关文章
📜  可以从两个数组前面删除的最大元素,使得它们的总和最多为 K

📅  最后修改于: 2021-09-17 07:00:22             🧑  作者: Mango

给定一个整数K和两个由NM 个整数组成的数组A[]B[] ,任务是根据以下规则最大化可以从任一数组前面删除的元素数:

  • 从数组A[]B[]的前面移除一个元素,使得被移除元素的值最多为 K
  • K的值减少所移除元素的值。

例子:

方法:给定的问题可以通过使用前缀和和二进制搜索找到的全部项目可能J可采取从堆栈B,从堆栈一个的项目后,返回的结果是(i + j)的最大值来解决。按照以下步骤解决给定的问题:

  • 查找数组A[]B[] 的前缀和。
  • 初始化一个变量,比如count0 ,它存储可以采取的最大项目。
  • 使用变量i在范围[0, N] 上遍历数组A[]并执行以下步骤:
    • 如果A[i]的值大于K ,则跳出循环。
    • 从堆栈A中取出i 个项目后的剩余金额存储在一个变量中, remK – A[i]
    • 对数组B执行二分搜索,以找到可以从堆栈B 中rem数量获取的最大项目j (在从堆栈A 中获取i 个元素之后)。
    • i + j的最大值存储在变量count 中
  • 完成以上步骤后,打印count的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum number
// of items that can be removed from
// both the arrays
void maxItems(int n, int m, int a[],
              int b[], int K)
{
    // Stores the maximum item count
    int count = 0;
 
    // Stores the prefix sum of the
    // cost of items
    int A[n + 1];
    int B[m + 1];
 
    // Insert the item cost 0 at the
    // front of the arrays
    A[0] = 0;
    B[0] = 0;
 
    // Build the prefix sum for
    // the array A[]
    for (int i = 1; i <= n; i++) {
 
        // Update the value of A[i]
        A[i] = a[i - 1] + A[i - 1];
    }
 
    // Build the prefix sum for
    // the array B[]
    for (int i = 1; i <= m; i++) {
 
        // Update the value of B[i]
        B[i] = b[i - 1] + B[i - 1];
    }
 
    // Iterate through each item
    // of the array A[]
    for (int i = 0; i <= n; i++) {
 
        // If A[i] exceeds K
        if (A[i] > K)
            break;
 
        // Store the remaining amount
        // after taking top i elements
        // from the array A
        int rem = K - A[i];
 
        // Store the number of items
        // possible to take from the
        // array B[]
        int j = 0;
 
        // Store low and high bounds
        // for binary search
        int lo = 0, hi = m;
 
        // Binary search to find
        // number of item that
        // can be taken from stack
        // B in rem amount
        while (lo <= hi) {
 
            // Calculate the mid value
            int mid = (lo + hi) / 2;
            if (B[mid] <= rem) {
 
                // Update the value
                // of j and lo
                j = mid;
                lo = mid + 1;
            }
            else {
 
                // Update the value
                // of the hi
                hi = mid - 1;
            }
        }
 
        // Store the maximum of total
        // item count
        count = max(j + i, count);
    }
 
    // Print the result
    cout << count;
}
 
// Driver Code
int main()
{
    int n = 4, m = 5, K = 7;
    int A[n] = { 2, 4, 7, 3 };
    int B[m] = { 1, 9, 3, 4, 5 };
    maxItems(n, m, A, B, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
// Function to find the maximum number
// of items that can be removed from
// both the arrays
static void maxItems(int n, int m, int a[],
                     int b[], int K)
{
     
    // Stores the maximum item count
    int count = 0;
 
    // Stores the prefix sum of the
    // cost of items
    int A[] = new int[n + 1];
    int B[] = new int[m + 1];
 
    // Insert the item cost 0 at the
    // front of the arrays
    A[0] = 0;
    B[0] = 0;
 
    // Build the prefix sum for
    // the array A[]
    for(int i = 1; i <= n; i++)
    {
         
        // Update the value of A[i]
        A[i] = a[i - 1] + A[i - 1];
    }
 
    // Build the prefix sum for
    // the array B[]
    for(int i = 1; i <= m; i++)
    {
         
        // Update the value of B[i]
        B[i] = b[i - 1] + B[i - 1];
    }
 
    // Iterate through each item
    // of the array A[]
    for(int i = 0; i <= n; i++)
    {
         
        // If A[i] exceeds K
        if (A[i] > K)
            break;
 
        // Store the remaining amount
        // after taking top i elements
        // from the array A
        int rem = K - A[i];
 
        // Store the number of items
        // possible to take from the
        // array B[]
        int j = 0;
 
        // Store low and high bounds
        // for binary search
        int lo = 0, hi = m;
 
        // Binary search to find
        // number of item that
        // can be taken from stack
        // B in rem amount
        while (lo <= hi)
        {
             
            // Calculate the mid value
            int mid = (lo + hi) / 2;
            if (B[mid] <= rem)
            {
                 
                // Update the value
                // of j and lo
                j = mid;
                lo = mid + 1;
            }
            else
            {
                 
                // Update the value
                // of the hi
                hi = mid - 1;
            }
        }
 
        // Store the maximum of total
        // item count
        count = Math.max(j + i, count);
    }
 
    // Print the result
    System.out.print(count);
}
 
// Driver Code
public static void main (String[] args)
{
    int n = 4, m = 5, K = 7;
    int A[] = { 2, 4, 7, 3 };
    int B[] = { 1, 9, 3, 4, 5 };
     
    maxItems(n, m, A, B, K);
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program for the above approach
 
# Function to find the maximum number
# of items that can be removed from
# both the arrays
def maxItems(n, m, a, b, K):
     
    # Stores the maximum item count
    count = 0
 
    # Stores the prefix sum of the
    # cost of items
    A = [0 for i in range(n + 1)]
    B = [0 for i in range(m + 1)]
 
    # Build the prefix sum for
    # the array A[]
    for i in range(1, n + 1, 1):
         
        # Update the value of A[i]
        A[i] = a[i - 1] + A[i - 1]
 
    # Build the prefix sum for
    # the array B[]
    for i in range(1, m + 1, 1):
         
        # Update the value of B[i]
        B[i] = b[i - 1] + B[i - 1]
 
    # Iterate through each item
    # of the array A[]
    for i in range(n + 1):
         
        # If A[i] exceeds K
        if (A[i] > K):
            break
 
        # Store the remaining amount
        # after taking top i elements
        # from the array A
        rem = K - A[i]
 
        # Store the number of items
        # possible to take from the
        # array B[]
        j = 0
 
        # Store low and high bounds
        # for binary search
        lo = 0
        hi = m
 
        # Binary search to find
        # number of item that
        # can be taken from stack
        # B in rem amount
        while (lo <= hi):
 
            # Calculate the mid value
            mid = (lo + hi) // 2
             
            if (B[mid] <= rem):
                 
                # Update the value
                # of j and lo
                j = mid
                lo = mid + 1
 
            else:
                 
                # Update the value
                # of the hi
                hi = mid - 1
 
        # Store the maximum of total
        # item count
        count = max(j + i, count)
 
    # Print the result
    print(count)
 
# Driver Code
if __name__ == '__main__':
     
    n = 4
    m = 5
    K = 7
    A = [ 2, 4, 7, 3 ]
    B = [ 1, 9, 3, 4, 5 ]
     
    maxItems(n, m, A, B, K)
         
# This code is contributed by bgangwar59


C#
// C# program for the above approach
using System;
 
class GFG
{   
 
// Function to find the maximum number
// of items that can be removed from
// both the arrays
static void maxItems(int n, int m, int[] a,
                     int[] b, int K)
{
     
    // Stores the maximum item count
    int count = 0;
 
    // Stores the prefix sum of the
    // cost of items
    int[] A = new int[n + 1];
    int[] B= new int[m + 1];
 
    // Insert the item cost 0 at the
    // front of the arrays
    A[0] = 0;
    B[0] = 0;
 
    // Build the prefix sum for
    // the array A[]
    for(int i = 1; i <= n; i++)
    {
         
        // Update the value of A[i]
        A[i] = a[i - 1] + A[i - 1];
    }
 
    // Build the prefix sum for
    // the array B[]
    for(int i = 1; i <= m; i++)
    {
         
        // Update the value of B[i]
        B[i] = b[i - 1] + B[i - 1];
    }
 
    // Iterate through each item
    // of the array A[]
    for(int i = 0; i <= n; i++)
    {
         
        // If A[i] exceeds K
        if (A[i] > K)
            break;
 
        // Store the remaining amount
        // after taking top i elements
        // from the array A
        int rem = K - A[i];
 
        // Store the number of items
        // possible to take from the
        // array B[]
        int j = 0;
 
        // Store low and high bounds
        // for binary search
        int lo = 0, hi = m;
 
        // Binary search to find
        // number of item that
        // can be taken from stack
        // B in rem amount
        while (lo <= hi)
        {
             
            // Calculate the mid value
            int mid = (lo + hi) / 2;
            if (B[mid] <= rem)
            {
                 
                // Update the value
                // of j and lo
                j = mid;
                lo = mid + 1;
            }
            else
            {
                 
                // Update the value
                // of the hi
                hi = mid - 1;
            }
        }
 
        // Store the maximum of total
        // item count
        count = Math.Max(j + i, count);
    }
 
    // Print the result
    Console.Write(count);
}
 
 
// Driver code
public static void Main(String []args)
{
    int n = 4, m = 5, K = 7;
    int[] A = { 2, 4, 7, 3 };
    int[] B = { 1, 9, 3, 4, 5 };
     
    maxItems(n, m, A, B, K);
 
}
}
 
// This code is contributed by code_hunt.


Javascript


输出:
3

时间复杂度: O(N * log(M))
辅助空间: O(N + M)

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