📌  相关文章
📜  使所有数组元素等于1所需的成本

📅  最后修改于: 2021-05-17 17:35:09             🧑  作者: Mango

给定大小为N的二进制数组arr [] ,任务是查找使所有数组元素等于1所需的总成本,其中将任何0转换为1的成本等于在该0之前存在的1的计数。

例子:

天真的方法:解决给定问题的最简单方法是遍历数组arr []并计算每个包含0的索引之前存在的1 s的数量,并打印所有获得的成本之和。

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

高效方法:可以通过观察以下事实来优化上述方法:在将每个0转换为1之后,在每个0之前存在的1 s的计数由发生0的索引给出。因此,任务是遍历给定数组并在数组arr []中打印所有具有0 s的索引的所有和。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to calculate the cost required
// to make all array elements equal to 1
int findCost(int A[], int N)
{
    // Stores the total cost
    int totalCost = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
 
        // If current element is 0
        if (A[i] == 0) {
 
            // Convert 0 to 1
            A[i] = 1;
 
            // Add the cost
            totalCost += i;
        }
    }
 
    // Return the total cost
    return totalCost;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 0, 1, 0, 1, 0 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << findCost(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
 
class GFG{
 
// Function to calculate the cost required
// to make all array elements equal to 1
static int findCost(int[] A, int N)
{
     
    // Stores the total cost
    int totalCost = 0;
 
    // Traverse the array arr[]
    for(int i = 0; i < N; i++)
    {
         
        // If current element is 0
        if (A[i] == 0)
        {
             
            // Convert 0 to 1
            A[i] = 1;
 
            // Add the cost
            totalCost += i;
        }
    }
 
    // Return the total cost
    return totalCost;
}
 
// Driver Code
public static void main(String[] args)
{
    int[] arr = { 1, 0, 1, 0, 1, 0 };
    int N = arr.length;
 
    System.out.println(findCost(arr, N));
}
}
 
// This code is contributed by ukasp


Python3
# Python3 program for the above approach
 
# Function to calculate the cost required
# to make all array elements equal to 1
def findCost(A, N):
 
    # Stores the total cost
    totalCost = 0
 
    # Traverse the array arr[]
    for i in range(N):
 
        # If current element is 0
        if (A[i] == 0):
 
            # Convert 0 to 1
            A[i] = 1
 
            # Add the cost
            totalCost += i
 
    # Return the total cost
    return totalCost
 
# Driver Code
if __name__ == '__main__':
 
    arr = [ 1, 0, 1, 0, 1, 0 ]
    N = len(arr)
     
    print(findCost(arr, N))
     
# This code is contributed by Shivam Singh


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
  
// Function to calculate the cost required
// to make all array elements equal to 1
static int findCost(int []A, int N)
{
     
    // Stores the total cost
    int totalCost = 0;
 
    // Traverse the array arr[]
    for(int i = 0; i < N; i++)
    {
         
        // If current element is 0
        if (A[i] == 0)
        {
 
            // Convert 0 to 1
            A[i] = 1;
 
            // Add the cost
            totalCost += i;
        }
    }
 
    // Return the total cost
    return totalCost;
}
 
// Driver Code
public static void Main()
{
    int []arr = { 1, 0, 1, 0, 1, 0 };
    int N = arr.Length;
     
    Console.Write(findCost(arr, N));
}
}
 
// This code is contributed by SURENDRA_GANGWAR


Javascript


输出:
9

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