📜  将 K 大小子数组的所有元素从给定的三元数组转换为 0 的最小成本,子数组总和为成本

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

将 K 大小子数组的所有元素从给定的三元数组转换为 0 的最小成本,子数组总和为成本

给定一个包含N个整数的数组arr[] ,其中每个数组元素是0、12以及一个整数K ,任务是通过选择打印将数组的所有元素转换为0所需的最小成本一个大小为K的子数组,并在一次操作中将子数组的任何数组元素转换为0 ,其成本等于子数组元素的总和。

例子:

方法:可以根据以下观察解决给定的问题:

请按照以下步骤解决问题:

  • 初始化两个数组,例如pcount1[]pcount2[]{0} ,其中pcount1[i]pcount2[i]1 s 和2 s 的计数存储在[0, i] 范围内的前缀中。
  • 使用变量i遍历数组arr[]并执行以下操作:
    • pcount1[i]分配给pcount1[i+1]并将pcount2[i]分配给pcount2[i+1]
    • 如果arr[i]1 ,则增加pcount1[i+1] 。否则,如果arr[i]2 ,则pcount2[i+1]的增量计数。
  • 找到数组的总和并将其存储在一个变量中,比如sum。
  • 初始化两个变量,比如XY ,以将1 s 和2 s 的计数存储在子数组中,并且大小总和为K
  • 使用变量i迭代范围[K, N]并执行以下步骤:
    • 初始化两个变量,比如AB ,以在[iK, K-1]范围内的子数组中存储1 s 和2 s 的计数。
    • pcount1[i]-pcount1[iK]分配给A ,将pcount2[i]-pcount2[iK]分配给B
    • 如果A+2*B小于X+2*Y ,则将X更新为A并将Y更新为B
  • 最后,完成上述步骤后,将总成本打印为sum-(X+2*Y) + Y 2 +X*Y+Y+(X*(X+1))/2。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the min cost
int minCost(int arr[], int N, int K)
{
    // Stores the prefix count of 1s
    int pcount1[N + 1] = { 0 };
    // Stores the prefix count of 2s
    int pcount2[N + 1] = { 0 };
 
    // Traverse the array arr[]
    for (int i = 1; i <= N; i++) {
        pcount1[i] = pcount1[i - 1] + (arr[i - 1] == 1);
        pcount2[i] = pcount2[i - 1] + (arr[i - 1] == 2);
    }
 
    // Stores total sum of the array arr[]
    int sum = pcount1[N] + 2 * pcount2[N];
 
    // Stores the count of 1s in a subarray
    int X = N;
 
    // Stores the count of 2s in a subarray
    int Y = N;
 
    // Iterate over the range [K, N]
    for (int i = K; i <= N; i++) {
        int A = pcount1[i] - pcount1[i - K];
        int B = pcount2[i] - pcount2[i - K];
 
        // If current subarray sum is less
        // than X+2*Y
        if (A + 2 * B < X + 2 * Y) {
            X = A;
            Y = B;
        }
    }
 
    // Stores the total cost
    int total = sum - (X + 2 * Y) + Y * Y + X * Y + Y
                + (X * (X + 1)) / 2;
    // Return total
    return total;
}
 
// Driver Code
int main()
{
 
    // Input
    int arr[] = { 2, 0, 1, 1, 0, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int K = 3;
    // Function call
    cout << minCost(arr, N, K);
 
    return 0;
}


Java
// Java Program for the above approach
import java.io.*;
import java.util.Arrays;
class GFG
{
   
    // Function to find the min cost
    static int minCost(int arr[], int N, int K)
    {
       
        // Stores the prefix count of 1s
        int pcount1[] = new int[N + 1];
       
        // Stores the prefix count of 2s
        int pcount2[] = new int[N + 1];
        Arrays.fill(pcount1, 0);
        Arrays.fill(pcount2, 0);
 
        // Traverse the array arr[]
        for (int i = 1; i <= N; i++) {
            int k = 0;
            int l = 0;
            if (arr[i - 1] == 1)
                k = 1;
 
            if (arr[i - 1] == 2)
                l = 1;
 
            pcount1[i] = pcount1[i - 1] + k;
            pcount2[i] = pcount2[i - 1] + l;
        }
 
        // Stores total sum of the array arr[]
        int sum = pcount1[N] + 2 * pcount2[N];
 
        // Stores the count of 1s in a subarray
        int X = N;
 
        // Stores the count of 2s in a subarray
        int Y = N;
 
        // Iterate over the range [K, N]
        for (int i = K; i <= N; i++) {
            int A = pcount1[i] - pcount1[i - K];
            int B = pcount2[i] - pcount2[i - K];
 
            // If current subarray sum is less
            // than X+2*Y
            if (A + 2 * B < X + 2 * Y) {
                X = A;
                Y = B;
            }
        }
 
        // Stores the total cost
        int total = sum - (X + 2 * Y) + Y * Y + X * Y + Y
                    + (X * (X + 1)) / 2;
        // Return total
        return total;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
       
        // Input
        int arr[] = { 2, 0, 1, 1, 0, 2 };
        int N = arr.length;
        int K = 3;
        // Function call
 
        System.out.println(minCost(arr, N, K));
 
    }
}
 
      // This code is contributed by Potta Lokesh


Python3
# Py program for the above approach
 
# Function to find the min cost
def minCost(arr, N, K):
   
    # Stores the prefix count of 1s
    pcount1 = [0] * (N + 1)
     
    # Stores the prefix count of 2s
    pcount2 = [0] * (N+1)
     
    # Traverse the array arr[]
    for i in range(1,N+1):
        pcount1[i] = pcount1[i - 1] + (arr[i - 1] == 1)
        pcount2[i] = pcount2[i - 1] + (arr[i - 1] == 2)
 
    # Stores total sum of the array arr[]
    sum = pcount1[N] + 2 * pcount2[N]
 
    # Stores the count of 1s in a subarray
    X = N
 
    # Stores the count of 2s in a subarray
    Y = N
 
    # Iterate over the range [K, N]
    for i in range(K, N + 1):
        A = pcount1[i] - pcount1[i - K]
        B = pcount2[i] - pcount2[i - K]
 
        # If current subarray sum is less
        # than X+2*Y
        if (A + 2 * B < X + 2 * Y):
            X = A
            Y = B
 
    # Stores the total cost
    total = sum - (X + 2 * Y) + Y * Y + X * Y + Y + (X * (X + 1)) // 2
    # Return total
    return total
 
# Driver Code
if __name__ == '__main__':
   
    # Input
    arr= [2, 0, 1, 1, 0, 2]
    N =  len(arr)
    K = 3
     
    # Function call
    print (minCost(arr, N, K))
 
# This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find the min cost
static int minCost(int []arr, int N, int K)
{
    // Stores the prefix count of 1s
    int []pcount1 = new int[N + 1];
    Array.Clear(pcount1, 0, N + 1);
   
    // Stores the prefix count of 2s
    int []pcount2 = new int[N + 1];
    Array.Clear(pcount2, 0, N + 1);
 
    // Traverse the array arr[]
    for (int i = 1; i <= N; i++) {
        if(arr[i - 1] == 1){
          pcount1[i] = pcount1[i - 1] + 1;
        }
        else
          pcount1[i] = pcount1[i - 1];
           
         if(arr[i - 1] == 2){
          pcount2[i] = pcount2[i - 1] + 1;
        }
        else
          pcount2[i] = pcount2[i - 1];
        
    }
 
    // Stores total sum of the array arr[]
    int sum = pcount1[N] + 2 * pcount2[N];
 
    // Stores the count of 1s in a subarray
    int X = N;
 
    // Stores the count of 2s in a subarray
    int Y = N;
 
    // Iterate over the range [K, N]
    for (int i = K; i <= N; i++) {
        int A = pcount1[i] - pcount1[i - K];
        int B = pcount2[i] - pcount2[i - K];
 
        // If current subarray sum is less
        // than X+2*Y
        if (A + 2 * B < X + 2 * Y) {
            X = A;
            Y = B;
        }
    }
 
    // Stores the total cost
    int total = sum - (X + 2 * Y) + Y * Y + X * Y + Y
                + (X * (X + 1)) / 2;
    // Return total
    return total;
}
 
// Driver Code
public static void Main()
{
 
    // Input
    int []arr = { 2, 0, 1, 1, 0, 2 };
    int N = arr.Length;
    int K = 3;
   
    // Function call
    Console.Write(minCost(arr, N, K));
}
}
 
// This code is contributed by SURENDRA_GANGWAR.


Javascript
// Javascript program for the above approach
 
// Function to find the min cost
function minCost(arr, N, K)
{
 
    // Stores the prefix count of 1s
    let pcount1 = new Array(N + 1).fill(0);
     
    // Stores the prefix count of 2s
    let pcount2 = new Array(N + 1).fill(0);
 
    // Traverse the array arr[]
    for (let i = 1; i <= N; i++) {
        pcount1[i] = pcount1[i - 1] + (arr[i - 1] == 1);
        pcount2[i] = pcount2[i - 1] + (arr[i - 1] == 2);
    }
 
    // Stores total sum of the array arr[]
    let sum = pcount1[N] + 2 * pcount2[N];
 
    // Stores the count of 1s in a subarray
    let X = N;
 
    // Stores the count of 2s in a subarray
    let Y = N;
 
    // Iterate over the range [K, N]
    for (let i = K; i <= N; i++) {
        let A = pcount1[i] - pcount1[i - K];
        let B = pcount2[i] - pcount2[i - K];
 
        // If current subarray sum is less
        // than X+2*Y
        if (A + 2 * B < X + 2 * Y) {
            X = A;
            Y = B;
        }
    }
 
    // Stores the total cost
    let total = sum - (X + 2 * Y) + Y * Y + X * Y + Y
        + (X * (X + 1)) / 2;
         
    // Return total
    return total;
}
 
// Driver Code
 
// Input
let arr = [2, 0, 1, 1, 0, 2];
let N = arr.length;
let K = 3;
 
// Function call
document.write(minCost(arr, N, K));
 
// This code is contributed by gfgking.


输出
7

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