📌  相关文章
📜  最大化可以从任一数组末尾删除的数字计数,总和最多为 K

📅  最后修改于: 2022-05-13 01:56:10.546000             🧑  作者: Mango

最大化可以从任一数组末尾删除的数字计数,总和最多为 K

给定两个包含NM个整数的数组S1S2以及一个整数K ,任务是找到可以从任一给定数组的末尾删除的最大整数数,使得删除的元素的总和小于或等于到K

例子:

方法:上述方法可以在贪心方法的帮助下解决。这个想法是将给定的数组视为堆栈(因为只能从任何数组中删除最末端的元素)。使用两指针方法检查所有可能的有效操作序列。以下是要遵循的步骤:

  • 首先,考虑到S2不存在,检查可以得到多少个S1
  • 现在,将步骤 1 中获得的当前答案存储为最终答案。
  • 现在开始遍历S2并继续将元素插入到删除的整数集中。如果移除元素的总和在任意点超过K ,则移除S1中从最后一个包含的整数,直到总和小于或等于K。
  • 根据每个步骤中的当前答案更新最终答案,这实际上给出了所有可能的有效操作序列。

下面是上述方法的实现:

C++
// C++ program of the above approach
#include 
using namespace std;
  
// Function to return max profit
// from both stacks given as input
int Max_profit_maximisation(int Stack1[], int Stack2[],
                            int n, int m, int K)
{
    // Stores the final answer
    int maximum_result = 0;
  
    // Stores the pointer to the
    // current window in stack 1
    int index_for_Stack1 = 0;
  
    // Stores the pointer to the
    // current window in stack 2
    int index_for_Stack2;
  
    // Stores sum of current window
    int curr_sum = 0;
  
    // Case where snly stack 1
    // is considered
    while (index_for_Stack1 < n
           && curr_sum + Stack1[index_for_Stack1]
                  < K) {
        curr_sum += Stack1[index_for_Stack1];
        index_for_Stack1++;
    }
  
    // Update answer
    maximum_result = index_for_Stack1;
  
    // Traverse Stack 2 and insert
    // elements into the current
    // window one at a time
    while (index_for_Stack2 < m) {
        curr_sum += Stack2[index_for_Stack2];
        index_for_Stack2++;
  
        // curr_sum after removing
        // last elements from Stack1
        while (curr_sum > K
               && index_for_Stack1 > 0) {
            index_for_Stack1--;
            curr_sum -= Stack1[index_for_Stack1];
        }
  
        // Updating the answer
        maximum_result
            = max(maximum_result,
                  index_for_Stack1
                      + index_for_Stack2);
    }
  
    // Return Answer
    return maximum_result;
}
  
// Driver code
int main()
{
    int S1[] = { 12, 21, 102 };
    int S2[] = { 167, 244, 377, 56,
                 235, 269, 23 };
    int N = 3;
    int M = 7;
    int K = 1000;
  
    cout << Max_profit_maximisation(S1, S2, N, M, K);
  
    return 0;
}


Java
// Java program of the above approach
import java.util.*;
  
class GFG{
  
// Function to return max profit
// from both stacks given as input
static int Max_profit_maximisation(int Stack1[], int Stack2[],
                            int n, int m, int K)
{
    // Stores the final answer
    int maximum_result = 0;
  
    // Stores the pointer to the
    // current window in stack 1
    int index_for_Stack1 =0;
  
    // Stores the pointer to the
    // current window in stack 2
    int index_for_Stack2=Integer.MAX_VALUE;
  
    // Stores sum of current window
    int curr_sum = 0;
  
    // Case where snly stack 1
    // is considered
    while (index_for_Stack1 < n
           && curr_sum + Stack1[index_for_Stack1]
                  < K) {
        curr_sum += Stack1[index_for_Stack1];
        index_for_Stack1++;
    }
  
    // Update answer
    maximum_result = index_for_Stack1;
  
    // Traverse Stack 2 and insert
    // elements into the current
    // window one at a time
    while (index_for_Stack2 < m) {
        curr_sum += Stack2[index_for_Stack2];
        index_for_Stack2++;
  
        // curr_sum after removing
        // last elements from Stack1
        while (curr_sum > K
               && index_for_Stack1 > 0) {
            index_for_Stack1--;
            curr_sum -= Stack1[index_for_Stack1];
        }
  
        // Updating the answer
        maximum_result
            = Math.max(maximum_result,
                  index_for_Stack1
                      + index_for_Stack2);
    }
  
    // Return Answer
    return maximum_result;
}
  
// Driver code
public static void main(String[] args)
{
    int S1[] = { 12, 21, 102 };
    int S2[] = { 167, 244, 377, 56,
                 235, 269, 23 };
    int N = 3;
    int M = 7;
    int K = 1000;
  
    System.out.print(Max_profit_maximisation(S1, S2, N, M, K));
}
}
  
// This code is contributed by shikhasingrajput


Python
# Python program of the above approach
  
# Function to return max profit
# from both stacks given as input
def Max_profit_maximisation(Stack1, Stack2, n, m, K):
      
    # Stores the final answer
    maximum_result = 0
  
    # Stores the pointer to the
    # current window in stack 1
    index_for_Stack1 = 0
  
    # Stores the pointer to the
    # current window in stack 2
    index_for_Stack2 = 100**100
  
    # Stores sum of current window
    curr_sum = 0
  
    # Case where snly stack 1
    # is considered
    while (index_for_Stack1 < n
           and curr_sum + Stack1[index_for_Stack1]
                  < K):
        curr_sum += Stack1[index_for_Stack1]
        index_for_Stack1 += 1
  
    # Update answer
    maximum_result = index_for_Stack1
  
    # Traverse Stack 2 and insert
    # elements into the current
    # window one at a time
    while (index_for_Stack2 < m) :
        curr_sum += Stack2[index_for_Stack2]
        index_for_Stack2 += 1
  
        # curr_sum after removing
        # last elements from Stack1
        while (curr_sum > K
               and index_for_Stack1 > 0):
            index_for_Stack1 -= 1
            curr_sum -= Stack1[index_for_Stack1]
  
        # Updating the answer
        maximum_result = max(maximum_result,
                            index_for_Stack1
                            + index_for_Stack2)
  
    # Return Answer
    return maximum_result
  
# Driver Code
if __name__ == '__main__':
      
    S1 = [ 12, 21, 102 ];
    S2 = [ 167, 244, 377, 56,
                 235, 269, 23 ];
    N = 3;
    M = 7;
    K = 1000;
  
    print(Max_profit_maximisation(S1, S2, N, M, K))
  
    # This code is contributed by Samim Hossain Mondal.


C#
// C# program of the above approach
using System;
  
class GFG{
  
// Function to return max profit
// from both stacks given as input
static int Max_profit_maximisation(int []Stack1, int []Stack2,
                                   int n, int m, int K)
{
      
    // Stores the readonly answer
    int maximum_result = 0;
  
    // Stores the pointer to the
    // current window in stack 1
    int index_for_Stack1 =0;
  
    // Stores the pointer to the
    // current window in stack 2
    int index_for_Stack2=int.MaxValue;
  
    // Stores sum of current window
    int curr_sum = 0;
  
    // Case where snly stack 1
    // is considered
    while (index_for_Stack1 < n && 
           curr_sum + Stack1[index_for_Stack1] < K) 
    {
        curr_sum += Stack1[index_for_Stack1];
        index_for_Stack1++;
    }
  
    // Update answer
    maximum_result = index_for_Stack1;
  
    // Traverse Stack 2 and insert
    // elements into the current
    // window one at a time
    while (index_for_Stack2 < m)
    {
        curr_sum += Stack2[index_for_Stack2];
        index_for_Stack2++;
  
        // curr_sum after removing
        // last elements from Stack1
        while (curr_sum > K && index_for_Stack1 > 0)
        {
            index_for_Stack1--;
            curr_sum -= Stack1[index_for_Stack1];
        }
  
        // Updating the answer
        maximum_result = Math.Max(maximum_result,
                                  index_for_Stack1 + 
                                  index_for_Stack2);
    }
  
    // Return Answer
    return maximum_result;
}
  
// Driver code
public static void Main(String[] args)
{
    int []S1 = { 12, 21, 102 };
    int []S2 = { 167, 244, 377, 56,
                 235, 269, 23 };
    int N = 3;
    int M = 7;
    int K = 1000;
  
    Console.Write(Max_profit_maximisation(S1, S2, N, M, K));
}
}
  
// This code is contributed by shikhasingrajput


Javascript


输出
3

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